2011-07-07 141 views
0
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) 

我有:Django的查詢ManyToManyField

p1 = Product.objects.get(pk = 1) 

我想指望有多少次p1包含在likes領域中的所有用戶配置的對象。

怎麼樣?

回答

1
UserProfile.objects.filter(likes=p1).count() 

假設一個產品只能被每個配置文件喜歡一次。

+0

謝謝。還有'p1.userprofile_set.count()' –