Django - 遵循外键关系(即 SQL 中的 JOIN)

发布于 2024-07-30 00:01:39 字数 1399 浏览 13 评论 0原文

忙着玩 django,但有一件事似乎让我绊倒,那就是遵循外键关系。 现在,我在编写 SQL 方面拥有丰富的经验,因此我可以解决这个问题。 如果 ORM 不存在,则返回结果。

基本上,这是我想要返回的 SQL 查询

Select
  table1.id
  table1.text
  table1.user
  table2.user_name
  table2.url
from table1, table2
where table1.user_id = table2.id

我的模型类已定义为:

class Table1(models.Model):
    #other fields
    text        = models.TextField()
    user        = models.ForeignKey('table2')

class Table2(models.Model):
    # other fields
    user_name = models.CharField(max_length=50)
    url = models.URLField(blank=True, null=True)

我已经浏览了 django 网站上查询集、模型和视图的文档和参考。 但目前还不清楚如何做到这一点。

我还使用通用列表视图设置了 url,但希望从模板中的第二个表访问 user_name 字段。 我在 urls.py 中尝试了 select_lated ,也通过 shell 尝试过,但它似乎不起作用。 请参阅下面的示例。

urls 中的配置

url(r'^$','django.views.generic.list_detail.object_list', { 'queryset': Table1.objects.select_related() }),

在 shell 中

>>> a = Table1.objects.select_related().get(id=1)
>>> a.id
1
>>> a.user_name
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Table1' object has no attribute 'user_name'

所以基本上,

  • 我做错了什么?
  • 我错过了什么吗?
  • 将同一查询集中两个表中的字段传递到模板的最佳方法是什么(以便可以访问两个表中的字段)
  • 这可以使用通用视图来完成吗?

Busy playing with django, but one thing seems to be tripping me up is following a foreign key relationship. Now, I have a ton of experience in writing SQL, so i could prob. return the result if the ORM was not there.

Basically this is the SQL query i want returned

Select
  table1.id
  table1.text
  table1.user
  table2.user_name
  table2.url
from table1, table2
where table1.user_id = table2.id

My model classes have been defined as:

class Table1(models.Model):
    #other fields
    text        = models.TextField()
    user        = models.ForeignKey('table2')

class Table2(models.Model):
    # other fields
    user_name = models.CharField(max_length=50)
    url = models.URLField(blank=True, null=True)

I have been through the documentation and reference for querysets, models and views on the django website. But its still not clear on how to do this.

I have also setup the url with a generic list view, but would like to access the user_name field from the second table in the template. I tried select_related in urls.py and also via the shell but it does not seem to work. See examples below.

config in urls

url(r'^

At the shell

>>> a = Table1.objects.select_related().get(id=1)
>>> a.id
1
>>> a.user_name
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'Table1' object has no attribute 'user_name'

So basically,

  • What am i doing wrong?
  • Am i missing something?
  • What's the best way to pass fields from two tables in the same queryset to your template (So fields from both tables can be accessed)
  • Can this be done with generic views?
,'django.views.generic.list_detail.object_list', { 'queryset': Table1.objects.select_related() }),

At the shell

So basically,

  • What am i doing wrong?
  • Am i missing something?
  • What's the best way to pass fields from two tables in the same queryset to your template (So fields from both tables can be accessed)
  • Can this be done with generic views?

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

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

发布评论

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

评论(2

公布 2024-08-06 00:01:39

像这样的事情应该可行:

u = Table1.objects.get(id=1)
print u.id
print u.user.user_name

如果您想遵循外键,则必须明确执行。 当您从 Table1 检索对象时,您不会获得自动联接。 当您访问外键字段(本例中为 user)时,您只会从 Table2 中获取一个对象

Something like this should work:

u = Table1.objects.get(id=1)
print u.id
print u.user.user_name

If you want to follow a foreign key, you must do it explicitly. You don't get an automatic join, when you retrieve an object from Table1. You will only get an object from Table2, when you access a foreign key field, which is user in this example

不乱于心 2024-08-06 00:01:39

select_lated() 不会将第二个表结果直接添加到查询中,它只是“加载”它们,而不是懒惰地提供给您。 仅当您确定它可以提高您的网站性能时才应使用它。 要获取第二个表,您需要

a.user.username

在 Django 中编写以获取相关表,您需要通过您设计的外键来跟踪它们。 查询本身并不直接转化为SQL查询,因为它是“惰性的”。 它只会在您需要时执行您需要的 SQL。

如果您有 select_lated SQL,那么当您对 a 进行原始查询时,SQL 就会被执行。 但如果您没有 select_lated 那么它只会在您实际执行 a.user.username 时加载数据库。

select_related() do not add second table result directly to the query, it just "Loads" them, insead of giving to you lazily. You should only use it, when you are sure it can boost your site performance. To get second table you need to write

a.user.username

In Django to get related table you need to follow them through foreign-keys that you have designed. Query itself do not directly translate to SQL query, because it is "lazy". It will execute only SQL that you need and only when you need.

If you have select_related SQL would have been executed at the moment when you do original query for a. But if you didn't have select_related then it would load DB only when you actually execute a.user.username.

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