Django - Getting last object created, simultaneous filters

后端 未结 7 2113
花落未央
花落未央 2020-12-24 00:39

Apologies, I am completely new to Django and Python.

I have 2 questions. First, how would I go about getting the last object created (or highest pk) in a list of obj

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-24 01:21

    alternative for the latest object created:

    List.objects.all()[List.objects.count()-1]
    

    It is necessary to add an AssertionError for the case when there are no items in the list.

    except AssertionError:
       ...
    

提交回复
热议问题