Frage

I have a problem with inheritance on my models when adding fields via add_to_class(). I have a models File(models.Model) and Image(File) - these come from django-filer.

In my app I'm importing them and adding fields and methods:

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)

Image should inherit both of those but it gets only the method(s), not field(s):

>>> 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'

Any clue?

War es hilfreich?

Lösung

Your model's add_to_class does not add the field as an attribute. it just calls contribute_to_class on your field: django/db/models/base.py#L218

Your field's contribute_to_class does not do it either. It just adds the field to the model's _meta member: django/db/models/fields/__init__.py#L234

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top