Opened 15 years ago

Closed 15 years ago

#11618 closed (duplicate)

Child models overwrite data of Parent model

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

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?

Change History (1)

comment:1 by Karen Tracey, 15 years ago

Resolution: duplicate
Status: newclosed

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

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