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 niall, 14 years ago

Component: UncategorizedDatabase layer (models, ORM)
Keywords: model inheritance ORM added
Triage Stage: UnreviewedAccepted

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.

comment:2 by Vlastimil Zíma, 14 years ago

Cc: Vlastimil Zíma added

comment:3 by niall, 14 years ago

Owner: changed from nobody to niall
Status: newassigned

comment:5 by Graham King, 13 years ago

Severity: Normal
Type: Bug

comment:6 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 by Aymeric Augustin, 12 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 by Tim Graham, 9 years ago

Component: Database layer (models, ORM)Core (System checks)
Summary: Fieldname cannot be the same as the Modelname when using model inheritanceAdd validation for model field name the same as the model name when using model inheritance
Version: 1.2master

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()

comment:9 by Tim Graham, 9 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:10 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 4bc00de:

Fixed #14217 -- Added validation for field name collision when using model inheritance.

Note: See TracTickets for help on using tickets.
Back to Top