Opened 9 years ago
Closed 7 years ago
#26362 closed Bug (fixed)
Manually changing the parent of a child model is silently failing.
Reported by: | Keith Hostetler | Owned by: | Paulo |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
Change History (5)
follow-up: 2 comment:1 by , 9 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 9 years ago
Replying to 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 oldpk
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.
comment:3 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
The first point seems to be a valid issue. In particular, after assigning a new parent, the
pk
attribute is updated, but not theid
. I think this can be fixed but I'm not 100% sure.As for the second issue,
QuerySet.update()
is working for me.(make sense since the
Child
with the oldpk
no longer exists)