Django database query: How to get object by id?

前端 未结 6 1508
遇见更好的自我
遇见更好的自我 2021-01-30 19:05

Django automatically creates an id field as primary key.

Now I need to get the object by this id.

object = Class.objects.filter() 

How

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 19:48

    If you want to get an object, using get() is more straightforward:

    obj = Class.objects.get(pk=this_object_id)
    

提交回复
热议问题