Django database query: How to get object by id?

user469652 picture user469652 · Nov 29, 2010 · Viewed 134.4k times · Source

Django automatically creates an id field as primary key.

Now I need to get the object by this id.

object = Class.objects.filter() 

How to write this filter?

Answer

iridescent picture iridescent · Nov 29, 2010

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

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