class UserProfile(models.Model):
user = models.ForeignKey(User, unique = True, related_name = 'user')
likes = models.ManyToManyField(Product, null = True, blank = True)
class Product(models.Model):
user = models.ForeignKey(User)
price = models.FloatField(default = 0)
I've:
p1 = Product.objects.get(pk = 1)
I want to count how many times p1
is contained in likes
field in all UserProfil开发者_高级运维e objects.
How?
UserProfile.objects.filter(likes=p1).count()
assuming that a product can only be liked one time by each profile.
精彩评论