Django: Get an object form the DB, or 'None' if nothing matches

后端 未结 8 1486
粉色の甜心
粉色の甜心 2020-11-30 02:45

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          


        
8条回答
  •  被撕碎了的回忆
    2020-11-30 03:14

    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)
    

提交回复
热议问题