【问题标题】:FieldError Cannot resolve keyword 'XXXX' into field, on wrong entityFieldError 无法将关键字“XXXX”解析为字段,在错误的实体上
【发布时间】: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


【解决方案1】:

class ProductFactory(Model):productscomponents 中具有相同的related_name (related_name='factory')

试着换一个。

【讨论】:

  • 嗨,同样的错误,但更新了相关名称:“无法将关键字 'factory_product' 解析到字段中。选项有:活动、文章、条形码、[...]”
  • 所以删除related_name
  • 同样的错误,默认的related_name(我假设由django选择):“无法将关键字'productfactory'解析为字段。选项有:活动、文章、条形码、[...]”跨度>
  • 在我的 django (1.7.1) 上一切正常(使用不同的related_name)奇怪。
  • 在同一个模型上有多个 ManyToMany 时可能会更正错误?知道它在哪个版本的 django 中得到纠正会很有趣。
猜你喜欢
  • 2013-10-09
  • 2021-05-23
  • 2019-05-26
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
相关资源
最近更新 更多