[Django] #32524: unexpected behavior when using gettattr for related object

25 views
Skip to first unread message

Django

unread,
Mar 8, 2021, 5:49:44 AM3/8/21
to django-...@googlegroups.com
#32524: unexpected behavior when using gettattr for related object
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Assuming we have a model like that:

{{{
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="profile",
on_delete=models.CASCADE)
...
}}}

An user may not have an UserProfile instance so we will use such logic:

{{{
profile = getattr(request.user, "profile", UserProfile(user=request.user))
}}}

But django will always return the default value no matter
`request.user.profile` exists or not.

Here is the poc:

{{{
In [1]: u = User.objects.get(username='s')

In [2]: u.profile
Out[2]: <UserProfile: UserProfile object (5)>

In [3]: getattr(u, 'profile', UserProfile())
Out[3]: <UserProfile: UserProfile object (5)>

In [4]: getattr(u, 'profile', UserProfile(user=u))
Out[4]: <UserProfile: UserProfile object (None)>

}}}

the problem is `UserProfile(user=u)` will update the `User.profile` when
we init `UserProfile`.

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

Django

unread,
Mar 8, 2021, 6:08:19 AM3/8/21
to django-...@googlegroups.com
#32524: unexpected behavior when using gettattr for related object
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by elonzh):

* needs_docs: 0 => 1


Old description:

> Assuming we have a model like that:
>
> {{{
> class UserProfile(models.Model):
> user = models.OneToOneField(User, related_name="profile",
> on_delete=models.CASCADE)
> ...
> }}}
>
> An user may not have an UserProfile instance so we will use such logic:
>
> {{{
> profile = getattr(request.user, "profile",
> UserProfile(user=request.user))
> }}}
>
> But django will always return the default value no matter
> `request.user.profile` exists or not.
>
> Here is the poc:
>
> {{{
> In [1]: u = User.objects.get(username='s')
>
> In [2]: u.profile
> Out[2]: <UserProfile: UserProfile object (5)>
>
> In [3]: getattr(u, 'profile', UserProfile())
> Out[3]: <UserProfile: UserProfile object (5)>
>
> In [4]: getattr(u, 'profile', UserProfile(user=u))
> Out[4]: <UserProfile: UserProfile object (None)>
>
> }}}
>
> the problem is `UserProfile(user=u)` will update the `User.profile` when
> we init `UserProfile`.

New description:

Assuming we have a model like that:

{{{
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="profile",
on_delete=models.CASCADE)
...
}}}

An user may not have an UserProfile instance so we will use such logic:

{{{
profile = getattr(request.user, "profile", UserProfile(user=request.user))
}}}

But django will always return the default value no matter
`request.user.profile` exists or not.

Here is the poc:

{{{
In [1]: u = User.objects.get(username='s')

In [2]: u.profile
Out[2]: <UserProfile: UserProfile object (5)>

In [3]: getattr(u, 'profile', UserProfile())
Out[3]: <UserProfile: UserProfile object (5)>

In [4]: getattr(u, 'profile', UserProfile(user=u))
Out[4]: <UserProfile: UserProfile object (None)>

}}}

the problem is `UserProfile(user=u)` will update the `u.profile` when we
init `UserProfile`.

I know this issue is caused by design, maybe we should update the document
for such case.

--

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

Django

unread,
Mar 8, 2021, 6:16:39 AM3/8/21
to django-...@googlegroups.com
#32524: unexpected behavior when using gettattr for related object
-------------------------------------+-------------------------------------
Reporter: elonzh | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

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


Comment:

> The problem is `UserProfile(user=u)` will update the User.profile when
we init UserProfile.

As far as I'm aware this is an expected behavior to keep relationships in
sync. I don't think there is anything that we could improve in docs, there
is also
[https://docs.djangoproject.com/en/3.1/topics/db/examples/one_to_one/#one-
to-one-relationships an example] which shows this behavior:

{{{
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')
>>> p1.save()
>>> p2 = Place(name='Ace Hardware', address='1013 N. Ashland')
>>> p2.save()
>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)
>>> r.save()
>>> r.place
<Place: Demon Dogs the place>
>>> p1.restaurant
<Restaurant: Demon Dogs the restaurant>
}}}


You can use `hasattr()` or catch `AttributeError`.

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

Reply all
Reply to author
Forward
0 new messages