#33008: Document prefetch_related for GenericForeignKey - Object doesn't exist
behaviour
--------------------------------------+------------------------------------
Reporter: Martin Svoboda | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.contenttypes | Version: 3.2
Severity: Normal | Resolution:
Keywords: Documentation | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Simon Charette):
Thanks for trying it out.
There must be a way to avoid this assignment overriding somehow as this
analogous situation doesn't result in attribute loss
{{{#!python
class UserRef(models.Model):
user = models.ForeignKey(User, models.DO_NOTHING, null=True,
db_constraint=False)
UserRef.objects.create(user_id=42)
ref = UserRef.objects.prefetch_related('user')[0]
assert ref.user is None
assert ref.user_id == 42
}}}
The distinction between the two is due to
[https://github.com/django/django/blob/3a6431db5461b88e691d770abdba6a2793ef699d/django/db/models/query.py#L1933-L1941
this branch] where `GenericForeignKey.get_prefetch_queryset` sets
`is_descriptor` to `True`.
I haven't tried it out but I suspect that switching `is_descriptor` to
`False`
[https://github.com/django/django/blob/3a6431db5461b88e691d770abdba6a2793ef699d/django/contrib/contenttypes/fields.py#L216
instead] now that `GenericForeignKey` has been changed to use the fields
cache (bfb746f983aa741afa3709794e70f1e0ab6040b5) would address your issue
and unify behaviours.
--
Ticket URL: <https://code.djangoproject.com/ticket/33008#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/071.0382e9c645735ad98d6e6945fdf8bde0%40djangoproject.com.