#23465: Inheritance destroying data in original model
-------------------------------------+-------------------------------------
     Reporter:  leandropls           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.6
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |             Triage Stage:
     Keywords:                       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by leandropls:

Old description:

> I've created the following model:
>
> school/models.py:
> {{{#!python
> 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:
>
> {{{#!python
> >>> 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.

New description:

 I've created the following model:

 school/models.py:
 {{{#!python
 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:

 {{{#!python
 >>> 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 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. 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.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/23465#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.2c1eeb7304941b180b1b9ff01bf049e9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to