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对象中p1
字段中包含likes
次的次数。
如何?
答案 0 :(得分:1)
UserProfile.objects.filter(likes=p1).count()
假设每个配置文件只能一次性浏览产品。