django 3 表查询集

django 3 tables queryset

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

的图像

例如,如果我想 select 来自多个 table 的特定列和数据库 table..

select vlans.name, vlans.vlan_id, gateways.vip, gateways.master, gateways.vhid, subnets.subnet, subnets.dhcp 来自 vlan、子网、网关,其中 vlans.vlan_id=2599 和 subnets.vlan_id=vlans.vlan_id 和 gateways.vlan_id = vlans.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>

enter image description here

我能够将它们连接在一起,但问题是 returns 整组对象...有什么方法可以按值获取一个值吗?

    vlan_query_results = Vlans.objects.select_related('vlan_id').values_list('name', 'gateways__vip', 'gateways__backup', 'gateways__master', 'gateways__nat', 'gateways__vhid', 'subnets__dhcp', 'subnets__dns').order_by('vlan_id')

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

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

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