【发布时间】:2017-11-26 21:35:53
【问题描述】:
我有一个问题
这是我的代码
视图.py
class testView(ListView):
model = Photo
template_name = 'photo/photo_detail.html'
context_object_name = 'main_list'
def get_context_data(self, **kwargs):
context = super(testView, self).get_context_data(**kwargs)
context['extra_value'] = Album.objects.filter(Album__name=Photo.album.name)
return context
和model.py
class Album(models.Model):
name = models.CharField(max_length=50)
description = models.CharField('One Line Description', max_length=100, blank= True)
owner = models.ForeignKey(User, null=True)
Category = models.ForeignKey(Categories, null=True)
price = models.CharField(max_length=20, null=True)
productInfo = models.CharField(max_length=50, null=True)
objects = models.Manager()
second_manager = SecondAlbumManager()
class Meta:
ordering = ['name']
verbose_name = 'Album'
verbose_name_plural = 'Albums'
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('photo:album_detail', args=(self.id,))
@python_2_unicode_compatible
class Photo(models.Model):
album = models.ForeignKey(Album)
title = models.CharField(max_length=50)
image = ThumbnailImageField(upload_to='photo/%Y/%m')
description = models.TextField('Photo Description', blank=True)
upload_date = models.DateTimeField('Upload Date', auto_now_add=True)
owner = models.ForeignKey(User, null=True)
class Meta:
ordering = ['title']
verbose_name = 'Photo'
verbose_name_plural = 'Photos'
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('photo:test', args=(self.id,))
Photo 对象有 ForeignKey(Album) 所以,我想在模板中使用 Filtered Album 对象(photo_detail.html)
但是,我想将一个 Photo 对象的 Album 外部值与 Album__name 进行比较并将其输出到模板,但我不知道如何更改 Photo.album.name 部分。请告诉我。
【问题讨论】:
标签: python django templates filter