Opened 14 years ago
Closed 9 years ago
#14217 closed Bug (fixed)
Add validation for model field name the same as the model name when using model inheritance
Reported by: | willian | Owned by: | niall |
---|---|---|---|
Component: | Core (System checks) | Version: | dev |
Severity: | Normal | Keywords: | model inheritance, ORM |
Cc: | Vlastimil Zíma | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
class Place(models.Model): pass class Restaurant(Place): restaurant = models.CharField(max_length=100)
When accessing Restaurant in the admin, a ValueError is Raised:
ValueError at /admin/questionnaire/restaurant/add/
Cannot assign "": "Restaurant.restaurant" must be a "Restaurant" instance.
---
Change History (9)
comment:1 by , 14 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Keywords: | model inheritance ORM added |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 14 years ago
Cc: | added |
---|
comment:3 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:5 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 by , 9 years ago
Component: | Database layer (models, ORM) → Core (System checks) |
---|---|
Summary: | Fieldname cannot be the same as the Modelname when using model inheritance → Add validation for model field name the same as the model name when using model inheritance |
Version: | 1.2 → master |
This should include the case of child/parent models as described in #17043:
from django.db import models class A(models.Model): pass class B(A): pass class C(A): b = models.BooleanField()
Note:
See TracTickets
for help on using tickets.
The super class has an attribute for each sub class with the same sub class but lower cased. This behavior is documented in the be careful with related name section of the model docs. The child elements will inherit inherit this attributes causing errors when you try to initialise an instance of that model (it thinks the fields are all related fields rather than say a CharField).
I'm guessing the models class should check for this and throw an exception when a field with the same lower case name as a subclass is defined.