开发者

Django query for ManyToManyField

开发者 https://www.devze.com 2023-03-18 04:23 出处:网络
class UserProfile(models.Model): user = models.ForeignKey(User, unique = True, related_name = \'user\')
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消