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

后端 未结 8 1490
粉色の甜心
粉色の甜心 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:27

    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.

提交回复
热议问题