我有一个django应用程序,您想要显示跟踪到特定图库的所有图像,但我收到错误:' GalleryDetailsView1'对象没有属性' gallery'
模型
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 :(得分:0)
当您提出要求时,您必须以某种方式通过图库pk
。例如,作为网址的一部分,然后在视图中从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
参数应为"paint_set"
才能生效。