--
Ticket URL: <https://code.djangoproject.com/ticket/16176>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 1
* needs_docs: => 0
Comment:
I can confirm that this bug exists. It happens with CharFields, but not
ForeignKeys. I haven't actually checked, but I'm guessing that the problem
lies in the use of contribute_to_class, which would mean that the
attribute with the field's name would never actually be set on the class -
and thus the property would not be overridden... If that is the problem,
one possible solution would be to just always set the attribute with the
field's name to None.
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:1>
* cc: teraom (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:2>
* keywords: => xxxxx
* owner: nobody => anonymous
* has_patch: 0 => 1
* component: Database layer (models, ORM) => HTTP handling
* severity: Normal => Release blocker
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:3>
Comment (by metzlar@…):
I made this work by defining the property dynamically inside the parent
model definition:
{{{
class SomeTestModel(models.Model):
some_field = models.CharField(max_length=100)
class Meta:
abstract = True
@classmethod
def __new__(cls, *args, **kwargs):
inst = models.Model.__new__(cls, *args, **kwargs)
if cls is SomeTestModel:
inst.other_field = property(lambda x: "[OTHER] %s" %
getattr(x, 'some_field'))
return inst
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:3>
* owner: nobody => Thomas_Moronez
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:4>
* has_patch: 0 => 1
* needs_tests: 1 => 0
Comment:
Added a try ... except statement that throws a more descriptive error
message. That way the user can know the error lies within the use of the
@property descriptor and can simply modify the name of the field.
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:5>
Comment (by carljm):
I don't think the proposed error-reraising is a good idea. For one thing,
we can't be sure that the `AttributeError` is in fact due to a property
from an abstract base class, it could be due to a definition of
`__setattr__`, a metaclass, or possibly other even more diabolical things.
For another, when we can avoid it it's a bad idea to obscure the original
traceback by catching one exception and raising another one in its place.
melinath's suggestion to set all fields as class attributes so they
override descriptors from a parent class is certainly worth considering,
as it would make models behave more like regular Python classes. This
could result in some backwards-incompatibility; would need to look into it
more to get a clearer sense of what cases might cause trouble, but I think
it's probably acceptable if documented in the release notes.
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:6>
Comment (by Thomas_Moronez):
I modified the add_to_class method to check if the value of the attribute
is a property. If it is it sets the attribute to the class with a None
value. I took melinath's suggestion too set the field's name to None.This
does fix the bug, and does not break test in model_inheritance_regress.
However, as developer carljm, pointed out, backwards-incompatibility may
be present as the patch adds 4 unit test failures.
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:7>
* needs_better_patch: 0 => 1
Comment:
Patch no longer applies cleanly.
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:8>
Comment (by Phist0ne):
I stumbled upon this bug lately using django 1.8 and am willing to provide
a patch.
The main problem seems that creating an attribute containing a django
field on a model does not overwrite any descriptors (with `__set__`
defined) from ancestor classes, which does not match the behavior of
standard python classes.
It makes sense to me that the attributes corresponding to model fields are
not set until the class is instantiated, since the values for these fields
are not available on class creation.
Therefore my suggested solution would be very similar to Thomas_Moronez's
patch. What confuses me though is that the proposed patch seems to not
call contribute_to_class.
In my opinion there are three possible solutions:
* delete all class attributes that will be overwritten by fields later
(e.g. in add_to_class after the "contribute-to-class" check")
* delete class attributes that have a `__set__` method defined
* set the class attributes to a specific value (e.g. a to be written
class) to mark them as to be overwritten later (melinath's suggestion)
Is this still considered a bug and if so which one is the preferred
solution?
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:9>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
Comment:
[https://github.com/django/django/pull/14508 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
Fixed in 225d96533a8e05debd402a2bfe566487cc27d95f
--
Ticket URL: <https://code.djangoproject.com/ticket/16176#comment:11>