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

后端 未结 19 1601
清酒与你
清酒与你 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条回答
  •  忘掉有多难
    2020-11-28 01:45

    you could use exists with a filter:

    Content.objects.filter(name="baby").exists()
    #returns False or True depending on if there is anything in the QS
    

    just an alternative for if you only want to know if it exists

提交回复
热议问题