Django 查询 ManyToManyField
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
的次数。
如何?
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 UserProfile objects.
How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设每个配置文件只能喜欢一种产品一次。
assuming that a product can only be liked one time by each profile.