Opened 6 years ago

Closed 13 months 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: master
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 Changed 6 years ago by niall

Component: UncategorizedDatabase layer (models, ORM)
Keywords: model inheritance ORM added
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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 Changed 6 years ago by Vlastimil Zíma

Cc: Vlastimil Zíma added

comment:3 Changed 6 years ago by niall

Owner: changed from nobody to niall
Status: newassigned

comment:5 Changed 6 years ago by Graham King

Severity: Normal
Type: Bug

comment:6 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 Changed 14 months ago by Tim Graham

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 Changed 13 months ago by Tim Graham

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:10 Changed 13 months ago by Tim Graham <timograham@…>

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