带有连接的 Django ORM SELECT

发布于 2024-12-11 19:23:22 字数 1351 浏览 0 评论 0原文

ERR

django 中的模型:

class Key(models.Model):
    id     = models.AutoField(primary_key=True, blank=True)
    name   = models.CharField(max_length=50)


class Record(models.Model):
    id         = models.AutoField(primary_key=True, blank=True)
    project_id = models.IntegerField()
    name       = models.CharField(max_length=50)


class Value(models.Model):
    id        = models.AutoField(primary_key=True, blank=True)
    record    = models.ForeignKey(Record)
    key       = models.ForeignKey(Key)
    value     = models.CharField(max_length=255)

我需要从数据库中选择此数据:

NAME (from record)
and fields related with this record
[NAME (from key), VALUE (from value)]
[NAME (from key), VALUE (from value)]
[...]

我可以使用 django ORM 来制作这个选择? (例如在 SQL select 中看起来像这样)

SELECT
    `keeper_record`.`id` AS `record_id`,
    `keeper_record`.`name` AS `name`,
    `keeper_record`.`desc` AS `desc`,
    `keeper_key`.`name` AS `key_name`,
    `keeper_key`.`desc` AS `key_desc`,
    `keeper_value`.`value` AS `value_value`
FROM `keeper_record`
JOIN `keeper_value` ON `keeper_record`.`id` = `keeper_value`.`record_id`
JOIN `keeper_key` ON `keeper_key`.`id` = `keeper_value`.`key_id`
WHERE record_id = id

ERR

Models in django:

class Key(models.Model):
    id     = models.AutoField(primary_key=True, blank=True)
    name   = models.CharField(max_length=50)


class Record(models.Model):
    id         = models.AutoField(primary_key=True, blank=True)
    project_id = models.IntegerField()
    name       = models.CharField(max_length=50)


class Value(models.Model):
    id        = models.AutoField(primary_key=True, blank=True)
    record    = models.ForeignKey(Record)
    key       = models.ForeignKey(Key)
    value     = models.CharField(max_length=255)

I need to select from DB this data:

NAME (from record)
and fields related with this record
[NAME (from key), VALUE (from value)]
[NAME (from key), VALUE (from value)]
[...]

May I use django ORM to make this selection? (for example in SQL select looks like this)

SELECT
    `keeper_record`.`id` AS `record_id`,
    `keeper_record`.`name` AS `name`,
    `keeper_record`.`desc` AS `desc`,
    `keeper_key`.`name` AS `key_name`,
    `keeper_key`.`desc` AS `key_desc`,
    `keeper_value`.`value` AS `value_value`
FROM `keeper_record`
JOIN `keeper_value` ON `keeper_record`.`id` = `keeper_value`.`record_id`
JOIN `keeper_key` ON `keeper_key`.`id` = `keeper_value`.`key_id`
WHERE record_id = id

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

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

发布评论

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

评论(2

贱贱哒 2024-12-18 19:23:22

以下选择与特定记录 ID 相关的值。然后您可以按照外键来获取相关的记录和密钥。使用 select_lated 最小化数据库查找。

# Select all values related to a record in your view
record = Record.objects.get(pk=record_id)
values = Value.objects.filter(record=record).select_related()

# In your template
{% for value in values %}
{{ value.record.name }} - {{ value.key.name }} - {{ value.value }}
{% endfor %}

选择多个记录

在您的 SQL 中,您有 WHERE record_id = 1,因此我展示了如何获取特定记录的所有值。您还可以在一个查询中为多于一条记录选择值。

# filter all records which belong to the project with `project_id=1`
records = Record.objects.filter(project_id=1)
# select all values that belong to these records
values = Value.objects.filter(record__in=records).select_related().order_by('record')

The following selects the values related to particular record id. You can then follow the foreign keys to get the related record and key. Using select_related minimises database lookups.

# Select all values related to a record in your view
record = Record.objects.get(pk=record_id)
values = Value.objects.filter(record=record).select_related()

# In your template
{% for value in values %}
{{ value.record.name }} - {{ value.key.name }} - {{ value.value }}
{% endfor %}

Selecting more that one record

In your sql, you had WHERE record_id = 1, so I showed how to get all the values for a particular record. You could also select the values for more that one record in one query.

# filter all records which belong to the project with `project_id=1`
records = Record.objects.filter(project_id=1)
# select all values that belong to these records
values = Value.objects.filter(record__in=records).select_related().order_by('record')
£冰雨忧蓝° 2024-12-18 19:23:22

应该相对简单,因为您已经将外键绑定在一起。

Record.objects.select_related().filter(id = variable_that_stores_id)

您可以将其与 only 结合使用来限制您需要的字段想带回来。

Should be relatively straightforward, since you already have the foreign keys tied together.

Record.objects.select_related().filter(id = variable_that_stores_id)

You can combine that with only to limit the fields that you want to bring back.

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