#34884: Half bug/half enhancement : inconsistent behavior of get_or_create()
regarding related attributes cache
-------------------------------------+-------------------------------------
Reporter: Laurent Lyaudet | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: ORM get_or_create | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Laurent Lyaudet):
The previous patch lowered the running time of my automatic tests from 195
s to 181 s which is quite good for such a small patch.
I tried also a slightly more powerful patch but it yields no mesurable
gain.
The second patch on `get` can totally replace the previous patch, you
don't need both.
{{{
original_get = QuerySet.get
def patched_get(self, *args, **kwargs):
result = original_get(self, *args, **kwargs)
for key, value in kwargs.items():
try:
if isinstance(result._meta.get_field(key), ForeignKey):
# isinstance handles OneToOneField also.
setattr(result, key, value)
except FieldDoesNotExist:
pass
return result
QuerySet.get = patched_get
}}}
This patch could still be improved to handle args also.
But I don't see a use case where one use:
{{{
my_object = MyModel.objects.get(Q(related_object=related_object))
}}}
instead of
{{{
my_object = MyModel.objects.get(related_object=related_object)
}}}
And clearly it could not be patched if one use `~Q()` or `Q() | Q()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/34884#comment:2>
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/0107018ae7e6d7f1-9a46df8e-7ced-46d2-9a2c-47aedefd9745-000000%40eu-central-1.amazonses.com.