Example:
{{{
class Parent(models.Model):
field1 = models.TextField(default="abc")
class Child(Parent):
field2 = models.TextField(default="abc")
def change_parent():
child = Child.objects.get(id=1)
new_parent = Parent.objects.create()
print child.id # Prints 1
print child.parent_ptr.id # Prints 1
print new_parent.id # We'll say it is number 5
child.parent_ptr = new_parent
print child.id # Prints 1
print child.parent_ptr.id # Prints 5
child.save()
print child.id # Prints 1
print child.parent_ptr.id # Prints 1
}}}
This applies to the update function as well. I tried:
{{{
Child.objects.filter(id=1).update(parent_ptr=new_parent)
child = Child.objects.get(id=1)
print child.id # Prints 1
print child.parent_ptr.id # Prints 1
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26362>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted
Comment:
The first point seems to be a valid issue. In particular, after assigning
a new parent, the `pk` attribute is updated, but not the `id`. I think
this can be fixed but I'm not 100% sure.
{{{
>>> child.parent_ptr = new_parent
>>> child.pk
2
>>> child.id
1
}}}
As for the second issue, `QuerySet.update()` is working for me.
{{{
>>> child = Child.objects.create()
>>> child_pk = child.id
>>> new_parent = Parent.objects.create()
>>> Child.objects.filter(id=child.pk).update(parent_ptr=new_parent)
>>> Child.objects.get(pk=child_pk)
...
DoesNotExist: Child matching query does not exist.
}}}
(make sense since the `Child` with the old `pk` no longer exists)
--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:1>
Comment (by Macainian):
Replying to [comment:1 timgraham]:
> As for the second issue, `QuerySet.update()` is working for me.
> {{{
> >>> child = Child.objects.create()
> >>> child_pk = child.id
> >>> new_parent = Parent.objects.create()
> >>> Child.objects.filter(id=child.pk).update(parent_ptr=new_parent)
> >>> Child.objects.get(pk=child_pk)
> ...
> DoesNotExist: Child matching query does not exist.
> }}}
> (make sense since the `Child` with the old `pk` no longer exists)
Ah, yes. You are right. I didn't technically test that code, but was
written based upon what I actually did and I realize now that I simply had
a reference issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:2>
* owner: nobody => Paulo
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:3>
* has_patch: 0 => 1
Comment:
PR https://github.com/django/django/pull/8626
--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"c4329271607637373e42f081bd632e653aa99ea3" c4329271]:
{{{
#!CommitTicketReference repository=""
revision="c4329271607637373e42f081bd632e653aa99ea3"
Fixed #26362 -- Fixed update of the inherited id field of an object when
its parent changes.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:5>