Skip to content

Instantly share code, notes, and snippets.

@danilovmy
Created October 30, 2024 13:08
Show Gist options
  • Select an option

  • Save danilovmy/8835ba4f0f1aaac31d5b432b74bdfe13 to your computer and use it in GitHub Desktop.

Select an option

Save danilovmy/8835ba4f0f1aaac31d5b432b74bdfe13 to your computer and use it in GitHub Desktop.
Resolve error in Django querySet.get
from django.utils.timezone import now
from django.db.models import QuerySet
...
class RepairedQuerySet(QuerySet):
def get(self, *args, **kwargs):
timer = now()
try:
iterator = self.filter(*args, **kwargs)[:2].iterator()
instance = next(iterator, None)
if instance is None or next(iterator, None):
return super().get(*args, **kwargs)
return instance
finally:
print('overriden get', now() - timer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment