Opened 5 years ago

Closed 3 days 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: vzima 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 5 years ago by niall

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Keywords model inheritance ORM added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 5 years ago by vzima

  • Cc vzima added

comment:3 Changed 5 years ago by niall

  • Owner changed from nobody to niall
  • Status changed from new to assigned

comment:5 Changed 4 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:8 Changed 3 weeks ago by timgraham

  • Component changed from Database layer (models, ORM) to Core (System checks)
  • Summary changed from Fieldname cannot be the same as the Modelname when using model inheritance to Add validation for model field name the same as the model name when using model inheritance
  • Version changed from 1.2 to 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()

comment:9 Changed 4 days ago by timgraham

  • Has patch set
  • Triage Stage changed from Accepted to Ready for checkin

comment:10 Changed 3 days ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

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