Using get()
with Q
in Django raises MultipleObjectsReturned
. For context this was a method I wrote at work:
from django.db import models
# ...
def get_foobar(a, b):
try:
return Foobar.objects.get(
models.Q(foo=a) | models.Q(bar__in=[a, b])
)
except DoesNotExist:
return None
Which translate in SQL to:
SELECT * FROM foobar
WHERE foo = 'a' OR bar IN ('a', 'b')
LIMIT 1
And this works fine. Until it probably won't. To mitigate this you can instead return just the latest entry:
from django.db import models
# ...
def get_foobar(a, b):
try:
return Foobar.objects.filter(
models.Q(foo=a) | models.Q(bar__in=[a, b])
).latest("created_at")
except DoesNotExist:
return None
Or, append distinct()
. But I wanted to return None
explicitly so I stuck with the above.
Top comments (0)