Changes between Version 1 and Version 2 of Ticket #28210, comment 2


Ignore:
Timestamp:
May 17, 2017, 2:32:32 AM (7 years ago)
Author:
Ivaylo Donchev

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #28210, comment 2

    v1 v2  
     1Yeah, but it's not about the **_state.db**.Basically, if we have  these two models:
     2{{{
     3    class Parent(models.Model):
     4        pass
     5    class ChildA(Parent):
     6        pass
     7    class ChildB(Parent):
     8        pass
     9
     10}}}
     11and 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:
     12{{{
     13    parent = obj.parent_ptr  # parent._state.adding == True
     14    child_b = ChildB(parent_ptr=obj.parent_ptr)
     15    child_b.__dict__.update(parent)  # to set all required attributes (if any)
     16    child_b.full_clean()  # this will throw an error for all of the unique fields of the parent instance ("id"for example)
     17    child_b.save()
     18}}}
     19So, if you set {{{ obj.parent_ptr._state.adding = False }}} as soon as obj is created, the code above is working without errors.
     20Basically, 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.
Back to Top