Django数据库查询:如何通过id获取对象?

发布于 2024-10-05 06:20:08 字数 135 浏览 1 评论 0原文

Django 自动创建一个 id 字段作为主键。

现在我需要通过这个id来获取对象。

object = Class.objects.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心凉 2024-10-12 06:20:15

如果您没有某些 ID,例如 mysite.com/something/9182301,您可以使用 get_object_or_404 通过 from django.shortcuts import get_object_or_404 导入。

使用示例:

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

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)
追我者格杀勿论 2024-10-12 06:20:14

您可以使用:

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

这将返回一个查询集,即使它获取一个对象。
如果您只需要一个对象,请使用:

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

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="")
開玄 2024-10-12 06:20:14

你也可以这样做:

obj = ClassModel.get_by_id(object_id)

这可行,但我可能不确定 Django 2 是否支持它。

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.

满天都是小星星 2024-10-12 06:20:13

您还可以使用 get_object_or_404 django捷径。如果未找到对象,则会引发 404 错误。

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

慵挽 2024-10-12 06:20:12

我遇到了同样的问题,但原因不同:

Class.objects.get(id=1)

此代码引发了 ImportError 异常。令我困惑的是,下面的代码执行得很好,并按预期返回了结果集:

Class.objects.all()

get() 方法的回溯尾部:

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

读取 Django 的 loading.py,我得出的结论是,我的 settings.py 到我的应用程序的路径不正确,其中包含我的 Class 模型定义。我所要做的就是更正应用程序的路径,并且 get() 方法执行良好。

这是我的 settings.py 以及正确的路径

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

:)

所有的混乱都是因为我使用 Django 的 ORM 作为独立的,所以命名空间必须反映这一点。

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.

盗琴音 2024-10-12 06:20:11

如果你想获取一个对象,使用 get() 更简单:

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

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

obj = Class.objects.get(pk=this_object_id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文