Code

Opened 5 years ago

Closed 5 years ago

#11618 closed (duplicate)

Child models overwrite data of Parent model

Reported by: truebrain@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Given the next set of models:

class Project(models.Model):
  name = models.IntegerField()

class Subversion_Project(Project):
  revision = models.IntegerField()

class Mercurial_Project(Project):
  revision = models.CharField(max_length=64)
# This model is not used below, but it shows the intention of my database design

And the code:

project = Project(name = 1)
project.save()
sproject = Subversion_Project(project_ptr = project)
sproject.save()

The latter command overwrites the content of 'project' with all empty fields. When you print project.name or sproject.project.name before the save() it still is fine. After the save() it updates the project with empty values.

My solution is:

sproject = Subversion_Project(project_ptr = project)
for f in project._meta.local_fields: setattr(sproject, f.name, getattr(project, f.name))

Which is, putting it mildly, silly. Is this a bug, or do I expect too much of the system?

Attachments (0)

Change History (1)

comment:1 Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

It's a use case not currently supported by inheritance, already covered by #7623, I believe.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


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

 
Note: See TracTickets for help on using tickets.