Is there any Django function which will let me get an object form the database, or None if nothing matches?
Right now I’m using something like:
foo = Foo.objects.filter(bar=baz) foo = len(foo) > 0 and foo.get() or None
But that’s not very clear, and it’s messy to have everywhere.
In Django 1.6 you can use the
first() Queryset method. It returns the first object matched by the queryset, or None if there is no matching object.
p = Article.objects.order_by('title', 'pub_date').first()
There are two ways to do this;
try: foo = Foo.objects.get(bar=baz) except model.DoesNotExist: foo = None
Or you can use a wrapper:
def get_or_none(model, *args, **kwargs): try: return model.objects.get(*args, **kwargs) except model.DoesNotExist: return None
Call it like this
foo = get_or_none(Foo, baz=bar)
To add some sample code to sorki’s answer (I’d add this as a comment, but this is my first post, and I don’t have enough reputation to leave comments), I implemented a get_or_none custom manager like so:
from django.db import models class GetOrNoneManager(models.Manager): """Adds get_or_none method to objects """ def get_or_none(self, **kwargs): try: return self.get(**kwargs) except self.model.DoesNotExist: return None class Person(models.Model): name = models.CharField(max_length=255) objects = GetOrNoneManager()
And now I can do this:
bob_or_none = Person.objects.get_or_none(name='Bob')
You can also try to use django annoying (it has another useful functions!)
install it with:
pip install django-annoying from annoying.functions import get_object_or_None get_object_or_None(Foo, bar=baz)
Give Foo its custom manager. It’s pretty easy – just put your code into function in custom manager, set custom manager in your model and call it with
If you need generic function (to use it on any model not just that with custom manager) write your own and place it somewhere on your python path and import, not messy any more.
Whether doing it via a manager or generic function, you may also want to catch ‘MultipleObjectsReturned’ in the TRY statement, as the get() function will raise this if your kwargs retrieve more than one object.
Building on the generic function:
def get_unique_or_none(model, *args, **kwargs): try: return model.objects.get(*args, **kwargs) except (model.DoesNotExist, model.MultipleObjectsReturned), err: return None
and in the manager:
class GetUniqueOrNoneManager(models.Manager): """Adds get_unique_or_none method to objects """ def get_unique_or_none(self, *args, **kwargs): try: return self.get(*args, **kwargs) except (self.model.DoesNotExist, self.model.MultipleObjectsReturned), err: return None
Here’s a variation on the helper function that allows you to optionally pass in a
QuerySet instance, in case you want to get the unique object (if present) from a queryset other than the model’s
all objects queryset (e.g. from a subset of child items belonging to a parent instance):
def get_unique_or_none(model, queryset=None, *args, **kwargs): """ Performs the query on the specified `queryset` (defaulting to the `all` queryset of the `model`'s default manager) and returns the unique object matching the given keyword arguments. Returns `None` if no match is found. Throws a `model.MultipleObjectsReturned` exception if more than one match is found. """ if queryset is None: queryset = model.objects.all() try: return queryset.get(*args, **kwargs) except model.DoesNotExist: return None
This can be used in two ways, e.g.:
obj = get_unique_or_none(Model, *args, **kwargs)as previosuly discussed
obj = get_unique_or_none(Model, parent.children, *args, **kwargs)
I think that in most cases you can just use:
foo, created = Foo.objects.get_or_create(bar=baz)
Only if it is not critical that a new entry will be added in Foo table ( other columns will have the None/default values )