Opened 10 years ago

Last modified 10 years ago

#23465 closed Bug

Inheritance destroying data in original model — at Initial Version

Reported by: leandropls Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal 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

I've created the following model:

school/models.py:

from django.contrib.auth.models import User
(...)
class Parent(User):
    contract = models.ForeignKey(Contract)
    user = models.OneToOneField(User, parent_link = True, related_name = 'school_parent')

Now I'm trying to "promote" a regular django user into a school parent:

>>> from django.contrib.auth.models import User
>>> from school.models import Parent, Contract
>>> u = User(username = 'myuser')
>>> u.save()
>>> User.objects.all()
[<User: myuser>]
>>> c = Contract.objects.get(pk = 1)
>>> p = Parent(user = u, contract = c)
>>> p.save()
>>> User.objects.all()
[<User: >]
>>> 

Apparently, in "Parent" creation, the user "myuser" is being destroyed. Django docs show[1] that you can "attach" one model to other via OneToOneField the way I'm doing. It also says that multi-table inheritance automatically creates a OneToOneField[2]. But as I inherit from User and set its OneToOneField to an existing instance of the user, the specified existing instance gets destroyed. The expected result was that django simply made the association.

Change History (0)

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