我不明白我的模型上的这个错误。我的模型(django 1.5.4):
from django.db.models import Model
from django.db import models
from django.utils.translation import ugettext_lazy as _
class ProductFactory(Model):
name = models.CharField(_(u"Name"), max_length=128)
products = models.ManyToManyField('Product', through='ProductFactoryProduct', related_name='factory')
components = models.ManyToManyField('Product', through='ProductFactoryComponent', related_name='factory')
class ProductFactoryProduct(Model):
factory = models.ForeignKey('ProductFactory')
product = models.ForeignKey('Product')
quantity = models.IntegerField(_(u"Quantity"), default=1)
class ProductFactoryComponent(Model):
factory = models.ForeignKey('ProductFactory')
product = models.ForeignKey('Product')
quantity = models.IntegerField(_(u"Quantity"), default=1)
class Product(Model):
active = models.BooleanField(_(u"Active"), default=True)
barcode = models.CharField(_(u"Barcode"), max_length=32, blank=True, null=True)
[...] # Nothing about factory
当我尝试获取Factory.products或Factory.components:
factory = ProductFactory.objects.get(pk=factory_id)
factory.products.all()
抛出此错误:
/ stock / factory / view / 1
中的FieldError无法将关键字'factory'解析为字段。选择是:活跃, 文章,条形码,品牌,分类,代码,comsumtion_type,id,名称, 包装,产品参考,提供商,供应商,purchase_quantity, 数量,数量_with_ampute,stock_limit,stockmovement, stockmovementproduct,work_unit
注意:有效,文章,条形码[...]字段为产品实体字段。
我不明白为什么django试图在 Product 实体上使用工厂键而不是 ProductFactoryProduct 实体。我的模特错了?有什么问题?
修改:完整代码:http://pastebin.com/SaMMUjd6
答案 0 :(得分:0)
class ProductFactory(Model):
products
和components
具有相同的related_name
(related_name='factory'
)
尝试更改一个。