当我要求模型管理器获取一个对象时,当没有匹配的对象时,它会引发DoesNotExist。

go = Content.objects.get(name="baby")

而不是DoesNotExist,我怎么能去是None代替?


当前回答

如果发生这样的事呢?

go = (Content.objects.filter(name="value") or [None])[0]

其他回答

您可以为此创建一个通用函数。

def get_or_none(classmodel, **kwargs):
    try:
        return classmodel.objects.get(**kwargs)
    except classmodel.DoesNotExist:
        return None

如下所示:

go = get_or_none(Content,name="baby")

如果没有匹配的条目,go将为None,否则将返回Content条目。

注意:如果name="baby"返回了多个条目,将引发异常MultipleObjectsReturned。

你应该在数据模型上处理它以避免这种错误,但你可能更喜欢在运行时像这样记录它:

def get_or_none(classmodel, **kwargs):
    try:
        return classmodel.objects.get(**kwargs)
    except classmodel.MultipleObjectsReturned as e:
        print('ERR====>', e)

    except classmodel.DoesNotExist:
        return None

下面是helper函数的一个变体,它允许你有选择地传入一个QuerySet实例,以防你想从模型的所有对象QuerySet之外的QuerySet中获得唯一的对象(如果存在)(例如,从属于父实例的子项的子集中):

def get_unique_or_none(model, queryset=None, **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(**kwargs)
    except model.DoesNotExist:
        return None

这可以用在两种情况下,例如:

obj = get_unique_or_none(Model, **kwargs),如前所述 obj = get_unique_or_none(模型,父。孩子,* * kwargs)

如果发生这样的事呢?

go = (Content.objects.filter(name="value") or [None])[0]

Maybe更好用:

User.objects.filter(username=admin_username).exists()

来一片怎么样?它将解析到限制1。

go = Content.objects.filter(name="baby")[0]