django 3表查询集

django 3表查询集,django,django-models,django-database,Django,Django Models,Django Database,我从Postgresql遗留数据库中填充了3个表。 我能够从vlan表中获取一些数据,但我很难从3个不同的表中获取所有数据,并将它们放入我在图中创建的表中。我尝试了多个查询集,例如list(chain(nameOfTables..) 你能帮帮我吗?我已附上该表的图像 例如,如果要从多个表中选择特定的列和DB 从vlan、子网、vlan所在的网关中选择vlan.name、vlan.vlan_id、gateways.vip、gateways.master、gateways.vhid、subnets.

我从Postgresql遗留数据库中填充了3个表。 我能够从vlan表中获取一些数据,但我很难从3个不同的表中获取所有数据,并将它们放入我在图中创建的表中。我尝试了多个查询集,例如list(chain(nameOfTables..) 你能帮帮我吗?我已附上该表的图像

例如,如果要从多个表中选择特定的列和DB

从vlan、子网、vlan所在的网关中选择vlan.name、vlan.vlan_id、gateways.vip、gateways.master、gateways.vhid、subnets.subnet、subnets.dhcp。vlan_id=2599和subnets.vlan_id=vlan.vlan_id和gateways.vlan_id=vlan.vlan_id=vlan.vlan_id

Models.py

from django.db import models


class Gateways(models.Model):
    vip = models.GenericIPAddressField(primary_key=True)
    vlan = models.ForeignKey('Vlans', models.DO_NOTHING, blank=True, null=True)
    master = models.GenericIPAddressField(blank=True, null=True)
    backup = models.GenericIPAddressField(blank=True, null=True)
    nat = models.GenericIPAddressField(blank=True, null=True)
    vhid = models.IntegerField(unique=True, blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'gateways'


class Subnets(models.Model):
    subnet = models.TextField(primary_key=True)  # This field type is a guess.
    vlan = models.ForeignKey('Vlans', models.DO_NOTHING, blank=True, null=True)
    dhcp = models.NullBooleanField()
    dhcp_start = models.GenericIPAddressField(blank=True, null=True)
    dhcp_end = models.GenericIPAddressField(blank=True, null=True)
    dns = models.GenericIPAddressField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'subnets'


class Vlans(models.Model):
    vlan_id = models.IntegerField(primary_key=True)
    allocated = models.NullBooleanField()
    name = models.TextField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'vlans'
Views.py

def niro_list(request):


gateways = Gateways.objects.all()
vlans = Vlans.objects.all()
subnets = Subnets.objects.all()



context = {
    'gateways': gateways,
    'vlans': vlans,
    'subnets': subnets,
}

return render(request, 'niro/niro_list.html', context)
niro_list.html

<div class="table-responsive-sm">
<table class="table">
  <thead>
    <tr>
      <th scope="col"></th>
      <th scope="col">#</th>
      <th scope="col">Name</th>
      <th scope="col">Type(Scenario)</th>
      <th scope="col">Interface</th>
      <th scope="col">Vlan ID</th>
      <th scope="col">RDP</th>
      <th scope="col">Network(CIDR)</th>
      <th scope="col">Nat IP</th>
      <th scope="col">DNS</th>
      <th scope="col">DHCP</th>
      <th scope="col">DHCP pool start</th>
      <th scope="col">DHCP pool end</th>
      <th scope="col">Master IP</th>
      <th scope="col">Backup IP</th>
      <th scope="col">VHID</th>


    </tr>
  </thead>
  <tbody>

  {% for instance in vlans %}

    <tr  #id='items-table'>

      <th scope="row" class="checkbox-row"><input type="checkbox" name="item" /></th>
        <th scope="row">{{ forloop.counter }}</th>
          <td class="item">{{ instance.name }}</td>
          <td class="item"></td>
          <td class="item">{{ instance.vlan_id }} </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"> </td>
          <td class="item"></td>
          <td class="item"> </td>
          <td class="item"></td>
          <td class="item"> </td>

{##}
{#        {% empty %}#}
{#            <td><p>No contetns</p></td>#}

    </tr>

    {% endfor %}


  </tbody>
</table>

如果您知道如何编写SQL查询(似乎知道),您可以自己运行SQL查询。在您的视图中放置类似的内容:

from django.db import connections
with connections.cursor() as cursor:
    cursor.execute(your_sql_query)
    res=cursor.fetchall()

并将SQL结果添加到模板的上下文中。然后在上下文中,您可以迭代您的结果。有关更多详细信息,请参阅。

谢谢您的回答。但是如果我想稍后通过我的应用程序更新DB。。如果我不遵循ORM,只使用原始查询,会不会有问题?
select
query不会导致任何问题。但是,如果执行
update
查询,请确保从数据库中重新获取python对象,因为模型实例缓存在内存中,不会反映您使用原始查询所做的更改。
from django.db import connections
with connections.cursor() as cursor:
    cursor.execute(your_sql_query)
    res=cursor.fetchall()