Creating a QuerySet based on a ManyToManyField in Django

Posted by River Tam on Stack Overflow See other posts from Stack Overflow or by River Tam
Published on 2012-12-05T05:02:46Z Indexed on 2012/12/05 5:03 UTC
Read the original article Hit count: 322

So I've got two classes; Picture and Tag that are as follows:

class Tag(models.Model):
        pics = models.ManyToManyField('Picture', blank=True)
        name = models.CharField(max_length=30)
        # stuff omitted

class Picture(models.Model):
        name = models.CharField(max_length=100)
        pub_date = models.DateTimeField('date published')
        tags = models.ManyToManyField('Tag', blank=True)
        content = models.ImageField(upload_to='instaton')
        #stuff omitted

And what I'd like to do is get a queryset (for a ListView) given a tag name that contains the most recent X number of Pictures that are tagged as such. I've looked up very similar problems, but none of the responses make any sense to me at all. How would I go about creating this queryset?

© Stack Overflow or respective owner

Related posts about python

Related posts about django