带有连接的 Django ORM SELECT
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下选择与特定记录 ID 相关的值。然后您可以按照外键来获取相关的记录和密钥。使用
select_lated
最小化数据库查找。选择多个记录
在您的 SQL 中,您有
WHERE record_id = 1
,因此我展示了如何获取特定记录的所有值。您还可以在一个查询中为多于一条记录选择值。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.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.应该相对简单,因为您已经将外键绑定在一起。
您可以将其与 only 结合使用来限制您需要的字段想带回来。
Should be relatively straightforward, since you already have the foreign keys tied together.
You can combine that with only to limit the fields that you want to bring back.