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

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

    In Django 1.6 you can use the first() Queryset method. It returns the first object matched by the queryset, or None if there is no matching object.

    Usage:

    p = Article.objects.order_by('title', 'pub_date').first()
    

提交回复
热议问题