[Django] #26362: Manually changing the parent of a child model is silently failing.

17 views
Skip to first unread message

Django

unread,
Mar 16, 2016, 6:11:42 AM3/16/16
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
----------------------------------------------+--------------------
Reporter: Macainian | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
To be honest, I have no idea if it is supposed to be allowed to set the
parent via the parent_ptr, but it isn't working. If it isn't allowed, it
should be reporting an error.

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.

Django

unread,
Mar 16, 2016, 1:42:41 PM3/16/16
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
-------------------------------------+-------------------------------------

Reporter: Macainian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Mar 16, 2016, 7:37:18 PM3/16/16
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
-------------------------------------+-------------------------------------

Reporter: Macainian | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jun 10, 2017, 11:57:15 AM6/10/17
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Paulo):

* owner: nobody => Paulo
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:3>

Django

unread,
Jun 10, 2017, 1:24:29 PM6/10/17
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Paulo):

* has_patch: 0 => 1


Comment:

PR https://github.com/django/django/pull/8626

--
Ticket URL: <https://code.djangoproject.com/ticket/26362#comment:4>

Django

unread,
Jun 26, 2017, 12:11:46 PM6/26/17
to django-...@googlegroups.com
#26362: Manually changing the parent of a child model is silently failing.
-------------------------------------+-------------------------------------
Reporter: Keith Hostetler | Owner: Paulo
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages