So I am trying to implement a user favorites system in Django. I need to store the time at which a Gallery object was favorited, so I am using Django's many to many through pattern. As you can see below I have a Gallery object which has the many to many relationship to Users who have favorited it, and the Favorite object which has a many to one with both User and Gallery. I would like to do some things like display a page with all the user's favorites on it, query a users favorites to see if a certain Gallery is in them, etc. So basically be able to get the galleries a certain user has favorited. What is the easiest way to do this with the ORM, or is this something I would need to write raw SQL for?
class Gallery(models.Model):
favoriters = models.ManyToManyField(
User, through='Favorite', null=True, related_name="favoriters")
def __unicode__(self):
return self.name
class Favorite(models.Model):
user = models.ForeignKey(User)
gallery = models.ForeignKey(Gallery)
date = models.DateField(auto_now_add=True)
Thanks.