Django: 从数据库中获取一个对象,如果没有匹配,则为“无”

是否有任何 Django 函数可以让我从数据库中得到一个对象,或者如果没有匹配的话,则无?

现在我用的是:

foo = Foo.objects.filter(bar=baz)
foo = len(foo) > 0 and foo.get() or None

但这并不是很清楚,而且到处都有是很混乱的。

63785 次浏览

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)

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 Foo.objects.your_new_func(...).

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.

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')

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

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.

Usage:

p = Article.objects.order_by('title', 'pub_date').first()

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)

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.:

  1. obj = get_unique_or_none(Model, *args, **kwargs) as previosuly discussed
  2. 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 )