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?
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?
I got here for the same problem, but for a different reason:
This code was raising an ImportError exception. What was confusing me was that the code below executed fine and returned a result set as expected:
Tail of the traceback for the
get()
method:Reading the code inside Django's
loading.py
, I came to the conclusion that mysettings.py
had a bad path to my app which contains myClass
model definition. All I had to do was correct the path to the app and theget()
method executed fine.Here is my
settings.py
with the corrected path:)
All the confusion was caused because I am using Django's ORM as a standalone, so the namespace had to reflect that.
If you want to get an object, using
get()
is more straightforward: