Code

Opened 4 years ago

Last modified 3 years ago

#14217 assigned Bug

Fieldname cannot be the same as the Modelname when using model inheritance

Reported by: willian Owned by: niall
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: model inheritance, ORM
Cc: vzima Triage Stage: Accepted
Has patch: no 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.
---

Attachments (1)

14217.patch (624 bytes) - added by gptvnt 3 years ago.
I have created a patch for meta class. This checks if any of the class attributes names conflict with the class name.

Download all attachments as: .zip

Change History (8)

comment:1 Changed 4 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 4 years ago by vzima

  • Cc vzima added

comment:3 Changed 4 years ago by niall

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

Changed 3 years ago by gptvnt

I have created a patch for meta class. This checks if any of the class attributes names conflict with the class name.

comment:4 Changed 3 years ago by gptvnt

Patch which I just added is breaking the test cases. Surely it needs more work. Could anyone please delete the attachment, otherwise, please ignore it.

comment:5 Changed 3 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:6 Changed 2 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:7 Changed 2 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as assigned
The owner will be changed from niall to anonymous. Next status will be 'assigned'
The ticket will be disowned. Next status will be 'new'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.