Django 3.2 - 在视图中查询多个模型(SQL 比较)
Django 3.2 - querie multiple Models in view (SQL comparison)
我对 django 很陌生,对 ORM 和视图查询有疑问。
我们已经将一些模型从我们的生产数据库迁移并同步到 django 数据库,但是我在将两个模型相互连接时遇到了问题。
例如models.py:
class SsdgSendung(models.Model):
sdg_sdgid = models.CharField(primary_key=True, max_length=30)
sdg_konz = models.CharField(max_length=12, blank=True, null=True)
class Meta:
db_table = 'ssdg_sendung'
class SsdaSdgadr(models.Model):
sda_sdgid = models.ForeignKey(SsdgSendung, on_delete=models.CASCADE, blank=True, null=True)
sda_satid = models.CharField(max_length=4, blank=True, null=True)
class Meta:
db_table = 'ssda_sdgadr'
unique_together = (('sda_sdgid', 'sda_satid'),)
示例数据
SsdgSendung:
- sdg_sdgid = BL-1237781-BL-1
- sdg_konz = 009874
SsdaSdgadr:
- sdg_sdgid = BL-1237781-BL-1
- sda_satid = IV
- sdg_sdgid = BL-1237781-BL-1
- sda_satid = CN
正确的“django”查询应该如何寻找这个等价物 SQL:
SELECT *
FROM
SsdgSendung
inner join SsdaSdgadr on sdg_sdgid = sda_sdgid and sda_satid = 'IV'
WHERE
sdg_konz = '1234'
我试过了,但我没有在模板上得到任何结果:
Sendungen = SsdgSendung.objects.filter(sdg_konz = current_user_konz).order_by('-sdg_datum').prefetch_related('sda_sdgid')
模板
{% for la_item in SDG_Overview %}
<tr>
<td>{{ la_item.sdg_sdgid }}</td> <!-- works well -->
<td>{{ la_item.sda_satid }}</td> <!-- don't work -->
</tr>
Sendungen=SsdaSdgadr.objects.filter(sda_sdgid__sdg_konz=current_user_konz).order_by('-sdg_datum')
模板
{% for la_item in SDG_Overview %}
<tr>
<td>{{ la_item.sdg_sdgid.sdg_sdgid }}</td> <!-- works well -->
<td>{{ la_item.sda_satid }}</td> <!-- will work -->
</tr>
这应该有效 Documentation
我对 django 很陌生,对 ORM 和视图查询有疑问。 我们已经将一些模型从我们的生产数据库迁移并同步到 django 数据库,但是我在将两个模型相互连接时遇到了问题。
例如models.py:
class SsdgSendung(models.Model):
sdg_sdgid = models.CharField(primary_key=True, max_length=30)
sdg_konz = models.CharField(max_length=12, blank=True, null=True)
class Meta:
db_table = 'ssdg_sendung'
class SsdaSdgadr(models.Model):
sda_sdgid = models.ForeignKey(SsdgSendung, on_delete=models.CASCADE, blank=True, null=True)
sda_satid = models.CharField(max_length=4, blank=True, null=True)
class Meta:
db_table = 'ssda_sdgadr'
unique_together = (('sda_sdgid', 'sda_satid'),)
示例数据
SsdgSendung:
- sdg_sdgid = BL-1237781-BL-1
- sdg_konz = 009874
SsdaSdgadr:
- sdg_sdgid = BL-1237781-BL-1
- sda_satid = IV
- sdg_sdgid = BL-1237781-BL-1
- sda_satid = CN
正确的“django”查询应该如何寻找这个等价物 SQL:
SELECT *
FROM
SsdgSendung
inner join SsdaSdgadr on sdg_sdgid = sda_sdgid and sda_satid = 'IV'
WHERE
sdg_konz = '1234'
我试过了,但我没有在模板上得到任何结果:
Sendungen = SsdgSendung.objects.filter(sdg_konz = current_user_konz).order_by('-sdg_datum').prefetch_related('sda_sdgid')
模板
{% for la_item in SDG_Overview %}
<tr>
<td>{{ la_item.sdg_sdgid }}</td> <!-- works well -->
<td>{{ la_item.sda_satid }}</td> <!-- don't work -->
</tr>
Sendungen=SsdaSdgadr.objects.filter(sda_sdgid__sdg_konz=current_user_konz).order_by('-sdg_datum')
模板
{% for la_item in SDG_Overview %}
<tr>
<td>{{ la_item.sdg_sdgid.sdg_sdgid }}</td> <!-- works well -->
<td>{{ la_item.sda_satid }}</td> <!-- will work -->
</tr>
这应该有效 Documentation