2015-05-09 61 views
0

我有一個Django應用程序要顯示跟蹤到一個特定的庫中的所有圖像,但我得到的錯誤:「GalleryDetailsView1」對象有沒有屬性「畫廊」外鍵在查看詳情

型號

class Gallery(models.Model): 
    title = models.CharField(max_length=200) 
    description = models.TextField() 
    image = ThumbnailerImageField(upload_to='paint/%Y/%m/%d') 

    class Meta: 
     verbose_name = "Gallery" 
     verbose_name_plural = " Galleries" 

    def __unicode__(self): 
     return self.title 


class Paint(models.Model): 
    AVAILABLE = "Available" 
    NOT_AVAILABLE = "Not available" 
    STATUS_PAINT = (
     (AVAILABLE, u"Dostępny"), 
     (NOT_AVAILABLE, u"Nie dostępny") 
    ) 
    title = models.CharField(max_length=200) 
    gallery = models.ForeignKey(Gallery, related_name='paint_set') 
    paint = ThumbnailerImageField(upload_to='paint/%Y/%m/%d') 
    price = models.CharField(max_length=50, blank=True, null=True) 
    status = models.CharField(choices=STATUS_PAINT, default=AVAILABLE, max_length=50) 

意見

class GalleryList(generic.ListView): 
    model = Gallery 
    context_object_name = "list" 
    template_name = "www/gallery_list.html" 


class GalleryDetailsView1(generic.DetailView): 
    context_object_name = "images1" 
    template_name = "www/gallery_details1.html" 

    def get_queryset(self): 
     return Gallery.objects.get(pk=self.kwargs['pk']).paint_set.all() 

模板:gallery_details1

{% extends "base.html" %} 
{% load thumbnail %} 

{% block content %} 


{% for i in images1 %} 

{{ i.title }} 
<hr> 
{% endfor %} 


{% endblock %} 

{% block content_bottom %}{% endblock content_bottom %} 

回答

0

你當你做你的要求要通過畫廊pk莫名其妙。例如,當你的URL的一部分,然後從self.kwargs['gallery_pk']得到它(gallery_pk位將取決於你的實現)的觀點:

url(r'^api/gallery/(?P<gallery_pk>\d+)/paintset$', GalleryDetailsView1.as_view()) 

然後,您可以返回

self.get_object().paint_set.all() 

爲您查詢集。

最後但並非最不重要的,related_name參數在gallery字段定義中應爲"paint_set"

+0

好吧,現在我得到404錯誤 – mark

+0

對不起,我必須問這個,但你測試過你的舊網址或新的?另外,你確定存在主鍵的對象嗎? – Ivan

+0

這是我的網址:'url(r'^ gallery1 /(?P \ d +)/ $',GalleryDetailsView1.as_view(),name =「gallery_details1」),' – mark