django : how to use filter option in this case?

Viewed 35

I have one question here is my code
view.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

and 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 object has ForeignKey(Album) so, I want to use Filtered Album objects in template(photo_detail.html)

However, I would like to compare the Album foreign value of one Photo object with Album__name and output it to the template, but I do not know how to change Photo.album.name part. Please let me know.

1 Answers

in the template you can simple use the property album, for example:

{% for photo in  object_list %}
    {{ photo.album.name }} created by {{ photo.album.user.username }}
{% endfor %}

read for details in docs

Related