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)
我:p1 = Product.objects.get(pk = 1)
我想计算在所有UserProfile对象中likes
字段中包含p1
的次数。
如何?
UserProfile.objects.filter(likes=p1).count()
假设一个产品只能被每个配置文件点赞一次。