Django Admin 中多表继承的 ValueError

发布于 2024-09-01 20:03:48 字数 1284 浏览 5 评论 0原文

我创建了两个继承模型 Entry 的新类:

class Entry(models.Model):
    LANGUAGE_CHOICES = settings.LANGUAGES

    language = models.CharField(max_length=2, verbose_name=_('Comment language'), choices=LANGUAGE_CHOICES)
    user = models.ForeignKey(User)
    country = models.ForeignKey(Country, null=True, blank=True)

    created = models.DateTimeField(auto_now=True)

class Comment(Entry):
    comment = models.CharField(max_length=2000, blank=True, verbose_name=_('Comment in English'))

class Discount(Entry):
    discount = models.CharField(max_length=2000, blank=True, verbose_name=_('Comment in English'))
    coupon = models.CharField(max_length=2000, blank=True, verbose_name=_('Coupon code if needed'))

通过 admin.site.register 将这些新模型添加到管理后,在尝试通过管理创建评论或折扣时,我收到 ValueError 。添加条目效果很好。

错误消息:

/admin/reviews/discount/add/ 处的 ValueError 无法分配“''”:“Discount.discount”必须是“Discount”实例。 请求方式:GET 请求网址: http://127.0.0.1:8000/admin/reviews/discount/添加/ 异常类型:值错误 异常值:
无法分配“''”:“Discount.discount”必须是“Discount”实例。 异常位置:set中的/Library/Python/2.6/site-packages/django/db/models/fields/lated.py,第211行 Python 可执行文件:/usr/bin/python Python版本:2.6.1

I created two new classes which inherit model Entry:

class Entry(models.Model):
    LANGUAGE_CHOICES = settings.LANGUAGES

    language = models.CharField(max_length=2, verbose_name=_('Comment language'), choices=LANGUAGE_CHOICES)
    user = models.ForeignKey(User)
    country = models.ForeignKey(Country, null=True, blank=True)

    created = models.DateTimeField(auto_now=True)

class Comment(Entry):
    comment = models.CharField(max_length=2000, blank=True, verbose_name=_('Comment in English'))

class Discount(Entry):
    discount = models.CharField(max_length=2000, blank=True, verbose_name=_('Comment in English'))
    coupon = models.CharField(max_length=2000, blank=True, verbose_name=_('Coupon code if needed'))

After adding these new models to admin via admin.site.register I'm getting ValueError when trying to create a comment or a discount via admin. Adding entries works fine.

Error msg:

ValueError at /admin/reviews/discount/add/
Cannot assign "''": "Discount.discount" must be a "Discount" instance.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/reviews/discount/add/
Exception Type: ValueError
Exception Value:
Cannot assign "''": "Discount.discount" must be a "Discount" instance.
Exception Location: /Library/Python/2.6/site-packages/django/db/models/fields/related.py in set, line 211
Python Executable: /usr/bin/python
Python Version: 2.6.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-09-08 20:03:48

出现此错误的原因是因为我使用了与模型名称已使用的相同的列名称。 Karen T. 在 Django 邮件列表中贡献了以下答案:

问题似乎是你有
在您的 Comment 模型中命名一个字段
具有相同的名称,只是小写。
Comment继承自Entry,使用
多表继承。这增加了一个
评论中的 OneToOneField 返回
进入,有一个副作用
向 Entry 添加“comment”属性。
这个属性可以让你
访问与相关的评论
进入的结果是
注释中的 OneToOneField 以及
默认它被赋予的名称
相关型号,全部小写。


当评论时就会出现问题
模型“继承”了所有
条目的字段/属性:
继承了 'comment' 属性
条目显然凌驾于
指定评论字段。那是
可能是一个错误,但似乎
从 1.0 开始就存在了。我没有
做了任何研究看看是否已经
报道称。

作为解决方法,您可以
将您的字段命名为除
型号名称全部小写,或者您
可以明确指定
子模型中的 OneToOneField,
指定parent_link=True 和
型号名称以外的其他名称
related_name 全部小写。

The reason for this error appeared was because I used same column name that was already used with model name. Karen T. contributed the following answer in Django mailing list:

The problem seems to be that you have
named a field in your Comment model
with the same name, only lower case.
Comment inherits from Entry, using
multi-table-inheritance. This adds a
OneToOneField in Comment back to
Entry, which has a side-effect of
adding a 'comment' attribute to Entry.
This is the attribute that lets you
access the Comment associated with the
Entry as a result of the
OneToOneField in Comment, and by
default it is given the name of the
related model, all-lowercase.

The
problem then occurs when the Comment
model "inherits' all the
fields/attributes of Entry: the
inherited 'comment' attribute from
Entry is apparently over-riding the
specified comment field. That's
probably a bug, but it appears to
have been there since 1.0. I have not
done any research to see if it's been
reported.

As a workaround you can
name your fields something other than
the model name all lowercased, or you
can explicitly specify the
OneToOneField in the child models,
specifying parent_link=True and
something other than the model name
all lowercased for related_name.

南城追梦 2024-09-08 20:03:48

Hunch 说你可以将 Entry 类声明为抽象类,除非你也需要一个 Entry 作为实际对象

...rest of Entry model here...
created = models.DateTimeField(auto_now_add=True) ## ONLY set date when created, not every save

class Meta:
   abstract = True

...methods for your model etc...

Hunch says you could do with declaring your Entry class as an abstract one unless you need an Entry as an actual object, too

...rest of Entry model here...
created = models.DateTimeField(auto_now_add=True) ## ONLY set date when created, not every save

class Meta:
   abstract = True

...methods for your model etc...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文