165

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?

user469652
  • 48,855
  • 59
  • 128
  • 165

6 Answers6

288

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

obj = Class.objects.get(pk=this_object_id)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
iridescent
  • 3,303
  • 1
  • 16
  • 10
  • 32
    FYI, `pk` is the preferred way to refer to the primary key for any model. The `id` field is only generated if the model author doesn't specifically designate a primary key. If the author *did* specify a primary key field that isn't named `id`, then there will not be an `id` field. – Craig Trader Nov 29 '10 at 04:18
  • Also FYI, `id` in a built-in function that returns the identity of an object. In a majority of cases referencing things by `id` will do the right thing e.g. `Class.objects.get(id=this_object_id)` work. But that's something to consider I guess. – Tom Jul 28 '16 at 20:45
26

I got here for the same problem, but for a different reason:

Class.objects.get(id=1)

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:

Class.objects.all()

Tail of the traceback for the get() method:

File "django/db/models/loading.py", line 197, in get_models
    self._populate()
File "django/db/models/loading.py", line 72, in _populate
    self.load_app(app_name, True)
File "django/db/models/loading.py", line 94, in load_app
    app_module = import_module(app_name)
File "django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named myapp

Reading the code inside Django's loading.py, I came to the conclusion that my settings.py had a bad path to my app which contains my Class model definition. All I had to do was correct the path to the app and the get() method executed fine.

Here is my settings.py with the corrected path:

INSTALLED_APPS = (
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    # ...
    'mywebproject.myapp',

)

All the confusion was caused because I am using Django's ORM as a standalone, so the namespace had to reflect that.

Lucio Paiva
  • 19,015
  • 11
  • 82
  • 104
10

You can also use get_object_or_404 django shortcut. It raises a 404 error if object is not found.

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47
4

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="")
3

You can also do:

obj = ClassModel.get_by_id(object_id)

This works, but there may I'm not sure if it's supported in Django 2.

Anthony
  • 931
  • 1
  • 13
  • 30
0

In case you don't have some id, e.g., mysite.com/something/9182301, you can use get_object_or_404 importing by from django.shortcuts import get_object_or_404.

Use example:

def myFunc(request, my_pk):
    my_var = get_object_or_404(CLASS_NAME, pk=my_pk)