Opened 9 years ago
Closed 9 years ago
#25910 closed Bug (fixed)
Model constructors accept read-only property names
Reported by: | Joshua Phillips | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.9 |
Severity: | Normal | Keywords: | model db |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
A model's constructor uses left-over keyword arguments (that weren't matched to fields) to set properties on the model instance. If the property's setter raises AttributeError (e.g. if it's a read-only property), that exception is ignored. This can lead to bugs when, for example, a field is changed to a property of the same name.
It looks like this behaviour (from 2007) might be unintentional: the try...except looks like it's only supposed to catch the AttributeError from getattr, not from the following settattr.
Fixing this is backwards-incompatible, but trying to set a read-only property in this way is most probably a bug anyway.
Attachments (1)
Change History (6)
comment:1 by , 9 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 9 years ago
#26027 was rightly marked as a duplicate of the present issue; it contains further evidence that the behavior is unintentional as well as a hint as to how a test could be constructed easily.
by , 9 years ago
Attachment: | django-model-ctor-prop.diff added |
---|
Patch to fix the problem, now with test
A test is also needed.