#28210: `<orm_object>._state.adding` behaviour and model inheritance
--------------------------------+--------------------------------------
     Reporter:  Ivaylo Donchev  |                    Owner:  nobody
         Type:  Uncategorized   |                   Status:  new
    Component:  Uncategorized   |                  Version:  1.11
     Severity:  Normal          |               Resolution:
     Keywords:                  |             Triage Stage:  Unreviewed
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+--------------------------------------

Comment (by Ivaylo Donchev):

 Yeah, but it's not about the **_state.db**.Basically, if we have  these
 two models:
 {{{
     class Parent(models.Model):
         pass
     class ChildA(Parent):
         pass
     class ChildB(Parent):
         pass

 }}}
 and we create "ChildA" object with  **obj = ChildA.objects.create()**,
 we'll have 2 new instances - ***(ChildA) obj*** and ***(Parent)
 obj.parent_ptr)***. Now, if you try to create "ChildB" instance as
 follows:
 {{{
     parent = obj.parent_ptr  # parent._state.adding == True
     child_b = ChildB(parent_ptr=obj.parent_ptr)
     child_b.__dict__.update(parent)  # to set all required attributes (if
 any)
     child_b.full_clean()  # this will throw an error for all of the unique
 fields of the parent instance ("id"for example)
     child_b.save()
 }}}
 So, if you set {{{ obj.parent_ptr._state.adding = False }}} as soon as obj
 is created, the code above is working without errors.
 Basically, the problem is that if we don't modify the _state's adding,
 it's no  possible to create a new instance with the same parent_ptr.

--
Ticket URL: <https://code.djangoproject.com/ticket/28210#comment:2>
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/069.e57e10075d5d4ec436c00ff1b0b8b800%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to