使用“通过”外键开发查询集

时间:2019-03-19 17:09:11

标签: django django-models

我正在寻找一个UserCarts的查询集,该查询集具有与特定UserService相关的UserCartItems:

例如,如果UserService是“ Painting”,我想获取所有具有UserCartItem且将“ Painting”作为外键的UserCarts的查询集。

Models.py

class UserService(models.Model):  
    title = models.ForeignKey(IndustryService, on_delete=models.CASCADE)

class UserCartItem(models.Model):    
    cart = models.ForeignKey("UserCart", on_delete=models.CASCADE, default=None) 
    title = models.ForeignKey(UserService, on_delete=models.CASCADE, default=None)

class UserCart(models.Model):
    items = models.ManyToManyField(UserService, through=UserCartItem)  

views.py

userservice = UserService.objects.get(id=2)
usercarts = UserCart.objects.filter(items__title=userservice)

这似乎不起作用。我收到以下错误:

Cannot query "X": Must be "IndustryService" instance.

感谢您的帮助!

0 个答案:

没有答案