Created
October 30, 2024 13:08
-
-
Save danilovmy/8835ba4f0f1aaac31d5b432b74bdfe13 to your computer and use it in GitHub Desktop.
Resolve error in Django querySet.get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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