我的代码:
products = Clothing.objects.select_related('product')
for product in products:
print(product.category+" "+product.price)
失败与:
AttributeError:“服装”对象没有属性“价格” [13 / Jan / 2019 13:31:53]“ GET / mens_product / HTTP / 1.1” 500 65348
当你有一个外键关系到Product
你的Clothing
模型,你可以得到所有相关产品的使用:
products = Product.objects.filter(clothing=a_clothing_instance)
如果您想使用简写形式,则可能正在寻找RelatedManager。在一种ForeignKey
情况下,您将拥有:
class Clothing(models.Model):
....
class Product(models.Model):
clothing = models.ForeignKey(clothing)
然后,您可以使用以下命令调用所有产品:
products = clothing.product_set.all()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句