Overwriting a property with field during model inheritance.
|Reported by:||czepiel.artur@…||Owned by:||Thomas_Moronez|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
Documentation says (in https://docs.djangoproject.com/en/1.3/topics/db/models/#field-name-hiding-is-not-permitted paragraph) that:
This restriction only applies to attributes which are Field instances. Normal Python attributes can be overridden if you wish. It also only applies to the name of the attribute as Python sees it: if you are manually specifying the database column name, you can have the same column name appearing in both a child and an ancestor model for multi-table inheritance (they are columns in two different database tables).
However.. I came up today with setup like this:
1 from django.db import models 2 3 # Create your models here. 4 5 class SomeTestModel(models.Model): 6 some_field = models.CharField(max_length=100) 7 8 class Meta: 9 abstract = True 10 11 @property 12 def other_field(self): 13 return "[OTHER] %s" % self.some_field 14 15 16 17 class OtherModel(SomeTestModel): 18 other_field = models.CharField(max_length=100) 19 20 21 class AndMoreOther(SomeTestModel): 22 not_important_field = models.CharField(max_length=100)
And then if you do:
>>> from testapp.models import * >>> o = OtherModel() Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/arturstudio/PROJEKTY/tempdjango/inh/src/django/django/db/models/base.py", line 357, in __init__ setattr(self, field.attname, val) AttributeError: can't set attribute
Since my models where a lot bigger and more complicate, it took me almost all day to figure out that the problem was a @property from a base model, and my suggestion is that there should be at least a warning somewhere (during model's init perhaps) that could be more precise about why attribute couldn't been set. (or attribute to which object (either Model or Field).
I tried it on 1.2 and 1.4 pre-alpha SVN-16338
To reproduce you just need to put the models.py from above in some app.
Change History (9)
comment:1 Changed 4 years ago by melinath
- Needs documentation unset
- Needs tests set
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:4 Changed 2 years ago by Thomas_Moronez
- Owner changed from nobody to Thomas_Moronez
- Status changed from new to assigned