[Django] #21401: But when saving an object pointed by another

24 views
Skip to first unread message

Django

unread,
Nov 7, 2013, 6:05:06 PM11/7/13
to django-...@googlegroups.com
#21401: But when saving an object pointed by another
----------------------------------------------+--------------------
Reporter: msbrogli@… | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.5
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
My models are:

{{{
class A(models.Model):
b = models.ForeignKey('B', null=True)
name = models.CharField(max_length=255)

class B(models.Model):
name = models.CharField(max_length=255)
}}}

The following code reproduces the problem:

{{{
a = A(name="Test A")
a.b = B(name="Test B")

a.b.save()
a.save()

a = A.objects.get(id=a.id)
assert a.b is not None
}}}

The assert fails! As I look inside the code, the field `b_id` is set only
when `a.b` is being set. So, the workaround is:

{{{
a = A(name="Test A")
a.b = B(name="Test B")

a.b.save()
a.b = a.b # workaround
a.save()

a = A.objects.get(id=a.id)
assert a.b is not None
}}}

Now it works as expected.

Is it a bug or a feature? I can't find anything in the docs.

If it is a bug, I guess the object should have a list of which objects are
pointing to it. So, when it is added, it should updated everyone id field.

--
Ticket URL: <https://code.djangoproject.com/ticket/21401>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 7, 2013, 6:05:54 PM11/7/13
to django-...@googlegroups.com
#21401: Bug when saving an object pointed by another
-------------------------------------+-------------------------------------

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

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by msbrogli@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/21401#comment:1>

Django

unread,
Nov 7, 2013, 6:50:52 PM11/7/13
to django-...@googlegroups.com
#21401: Bug when saving an object pointed by another
-------------------------------------+-------------------------------------
Reporter: msbrogli@… | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution: duplicate

Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #10811

--
Ticket URL: <https://code.djangoproject.com/ticket/21401#comment:2>

Reply all
Reply to author
Forward
0 new messages