How do I get the object if it exists, or None if it does not exist?

后端 未结 19 1498
清酒与你
清酒与你 2020-11-28 01:14

When I ask the model manager to get an object, it raises DoesNotExist when there is no matching object.

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


        
19条回答
  •  -上瘾入骨i
    2020-11-28 01:44

    It's one of those annoying functions that you might not want to re-implement:

    from annoying.functions import get_object_or_None
    #...
    user = get_object_or_None(Content, name="baby")
    

提交回复
热议问题