【发布时间】:2015-02-26 09:26:42
【问题描述】:
我不明白我的模型上有这个错误。我的模型(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 处的字段错误
无法将关键字“工厂”解析为字段。选择是:主动, 文章、条形码、品牌、类别、代码、comsumtion_type、id、名称、 包装、产品参考、供应商、供应商、购买数量、 数量, quantity_with_ampute, stock_limit, stockmovement, stockmovementproduct, work_unit
注意:活动、文章、条形码 [...] 字段是 产品 实体字段。
我不明白为什么 django 试图在 Product 实体而不是 ProductFactoryProduct 实体上使用 factory 键。我的模型错了?有什么问题?
编辑:完整代码:http://pastebin.com/SaMMUjd6
【问题讨论】:
-
此错误似乎与您发布的查询或模型无关。请发布完整的追溯、完整的相关视图和实际模型。
-
在 pastebin 页面上添加了完整代码。
-
在我的例子中,我导入了两个具有相同名称的模型,它返回了相同的错误。
标签: django