Django中的神奇的下划线(单下划线和双下划线)
- 跨表(别人的表)用双下划线,单表(自己的表)就用单下划线
-
刷新浏览器(效果图)
-
元组
-
代码(过程)修改的目录
- models.py
from django.db import models
# Create your models here.
# class Foo(models.Model):
# name = models.CharField(max_length=1)
# 公司
class Business(models.Model):
# id
caption = models.CharField(max_length=32)
code = models.CharField(max_length=32,null=True,default="SA")
# fk = models.ForeignKey('Foo')
# 主机
class Host(models.Model):
nid = models.AutoField(primary_key=True)
hostname = models.CharField(max_length=32,db_index=True)
# GenericIPAddressField(Field)字符串类型,Django Admin以及ModelForm中提供验证 Ipv4和Ipv6
ip = models.GenericIPAddressField(protocol="ipv4",db_index=True)
port = models.IntegerField()
b = models.ForeignKey(to="Business", to_field='id')
- views.py
from django.shortcuts import render,HttpResponse
from app01 import models
# Create your views here.
def business(request):
# 获取用户数据
v1 = models.Business.objects.all()
# QuerySet
# [obj(id,caption,code),obj(id,caption,code),obj(id,caption,code)]
v2 = models.Business.objects.all().values('id','caption')
# QuerySet
# [{'id':1,'caption':'运维部'},{...},{...},]
v3 = models.Business.objects.all().values_list('id', 'caption')
# QuerySet
# [(1,运维部),(2,开发)]
return render(request, 'business.html', {'v1': v1, 'v2': v2, 'v3':v3})
def host(request):
# 获取主机
v1 = models.Host.objects.filter(nid__gt=0)
# for row in v1:
# sep='\t'字段之间有间隔
# print(row.nid,row.hostname,row.ip,row.port,row.b_id,row.b.caption,row.b.code,row.b.id,sep='\t')
# print(row.b.fk.name)
v2 = models.Host.objects.filter(nid__gt=0).values('nid','hostname','b_id','b__caption')
# print(v2)
for row in v2:
print(row['nid'],row['hostname'], row['b_id'], row['b__caption'])
v3 = models.Host.objects.filter(nid__gt=0).values_list('nid', 'hostname', 'b_id', 'b__caption')
return render(request, 'host.html', {'v1': v1, 'v2':v2, 'v3':v3})
- host.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>业务线列表(对象)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>IP</th>
<th>端口</th>
<!--<th>业务线ID</th>-->
<th>业务线名称</th>
<!--<th>业务线编码</th>-->
</tr>
</thead>
<tbody>
{% for row in v1 %}
<tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
<!--<td>{{ row.nid }}</td>-->
<td>{{ row.hostname }}</td>
<td>{{ row.ip }}</td>
<td>{{ row.port }}</td>
<!--<td>{{ row.b_id }}</td>-->
<td>{{ row.b.caption }}</td>
<!--<td>{{ row.b.code }}</td>-->
</tr>
{% endfor %}
</tbody>
</table>
<h1>业务线列表(字典)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>业务线名称</th>
</tr>
</thead>
<tbody>
{% for row in v2 %}
<tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
<td>{{ row.hostname }}</td>
<td>{{ row.b__caption }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h1>业务线列表(元组)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>业务线名称</th>
</tr>
</thead>
<tbody>
{% for row in v3 %}
<tr hid="{{ row.0 }}" bid="{{ row.2 }}">
<td>{{ row.1 }}</td>
<td>{{ row.3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
- host.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>主机列表(对象)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>IP</th>
<th>端口</th>
<!--<th>业务线ID</th>-->
<th>业务线名称</th>
<!--<th>业务线编码</th>-->
</tr>
</thead>
<tbody>
{% for row in v1 %}
<tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
<!--<td>{{ row.nid }}</td>-->
<td>{{ row.hostname }}</td>
<td>{{ row.ip }}</td>
<td>{{ row.port }}</td>
<!--<td>{{ row.b_id }}</td>-->
<td>{{ row.b.caption }}</td>
<!--<td>{{ row.b.code }}</td>-->
</tr>
{% endfor %}
</tbody>
</table>
<h1>主机列表(字典)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>业务线名称</th>
</tr>
</thead>
<tbody>
{% for row in v2 %}
<tr hid="{{ row.nid }}" bid="{{ row.b_id }}">
<td>{{ row.hostname }}</td>
<td>{{ row.b__caption }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<h1>主机列表(元组)</h1>
<table border="1">
<thead>
<tr>
<!--<th>主机ID</th>-->
<th>主机名</th>
<th>业务线名称</th>
</tr>
</thead>
<tbody>
{% for row in v3 %}
<tr hid="{{ row.0 }}" bid="{{ row.2 }}">
<td>{{ row.1 }}</td>
<td>{{ row.3 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>