繁体   English   中英

django add_to_class()使模型继承/ MRO工作错误

[英]django add_to_class() making models inheritance/MRO work wrong

通过add_to_class()添加字段时,我的模型上的继承存在问题。 我有模型File(models.Model)Image(File) - 这些来自django-filer。

在我的应用程序中,我正在导入它们并添加字段和方法:

def method_x(self):
    print "x"

File.add_to_class("expiration_date", models.DateField(null=True, blank=True))
File.add_to_class("method_x", method_x)

图像应该继承这两者,但它只获取方法,而不是字段:

>>> some_file = File.objects.get(id=8)
>>> some_image = Image.objects.get(id=8)
>>>
>>> print some_file.expiration_date # this works
... None
>>>
>>> some_image.metgod_x() # this works
>>> x
>>>
>>> print some_image.expiration_date # and this not
Traceback (most recent call last):
    File "<console>", line 1, in <module>
AttributeError: 'Image' object has no attribute 'expiration_date'

任何线索?

您的模型的add_to_class不会将该字段添加为属性。 它只是调用contribute_to_class在你的领域: django/db/models/base.py#L218

你的字段的contribute_to_class也没有这样做。 它只是将字段添加到模型的_meta成员: django/db/models/fields/__init__.py#L234

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM