Django database query: How to get object by id?

前端 未结 6 1505
遇见更好的自我
遇见更好的自我 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:52

    You can use:

    objects_all=Class.objects.filter(filter_condition="")
    

    This will return a query set even if it gets one object. If you need exactly one object use:

    obj=Class.objects.get(conditon="")
    

提交回复
热议问题