Creating a QuerySet based on a ManyToManyField in Django
- by River Tam
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?