第二十章 Django数据库实战
第一课 获取单表单数据的三种方式:
urls.py中的路由代码:
path('busniess',views.busniess),
views.py中代码:
def busniess(req):
v1=models.busniess.objects.all()
print('v1:',v1)#是对象
v2=models.busniess.objects.all().values('id','caption')
print('v2:',v2)#是字典
v3=models.busniess.objects.values_list('id','caption')
print('v3',v3)
return render(req,'busniess.html',{'v1':v1,'v2':v2,'v3':v3})
Template目录下创建busniess.html文件,代码:
<ul>
{% for i in v1 %}
<li>{{ i.id }}-{{ i.caption }}-{{ i.code }}</li>
{% endfor %}
</ul>
<ul>
{% for i in v2 %}
<li>{{ i.id }}-{{ i.caption }}</li>
{% endfor %}
</ul>
<ul>
{% for i in v3 %}
<li>{{ i.0 }}-{{ i.1 }}</li>
{% endfor %}
</ul>
浏览器访问URL:http://127.0.0.1:8000/app02/busniess
首页显示:
1-运维-aa
2-销售-bb
3-开发-cc
4-渠道-dd
1-运维
2-销售
3-开发
4-渠道
1-运维
2-销售
3-开发
4-渠道
运行输出:
v1: <QuerySet [<busniess: busniess object (1)>, ...]>
v2: <QuerySet [{'id': 1, 'caption': '运维'},...]>
v3 <QuerySet [(1, '运维'), (2, '销售'), (3, '开发'), (4, '渠道')]>
第二课 一对多跨表操作
获取一对多表单数据:
1.建立两张表,一张主键表busniess,一张参照表host,Models.py中代码:
class busniess(models.Model):
caption=models.CharField(max_length=64)
code=models.CharField(max_length=32)
class Host(models.Model):
uid=models.AutoField(primary_key=True)
hostname=models.CharField(max_length=32,db_index=True)
ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)
port=models.IntegerField()
b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)
2.在命令行下执行创建完毕:
python manage.py makemigrations
python manage.py migrate
3.在两张表中自定义一些数据。
4.在urls.py中加入路径:path('host',views.host),
5.在views.py中代码:
def host(req):
v1=models.Host.objects.filter(uid__gt=0)
for row in v1:
print(row.uid,row.hostname,row.ip,row.port,row.b_id,row.b)
return HttpResponse('host')
6.浏览器访问:http://127.0.0.1:8000/app02/host 触发函数
7.后台输出:
1 c1.com 1.1.1.1 9900 1 busniess object (1)
2 c2.com 1.1.1.2 8800 1 busniess object (1)
3 c3.com 1.1.1.3 7700 2 busniess object (2)
8.修改5中代码:
def host(req):
v1=models.Host.objects.filter(uid__gt=0)
for row in v1:
print(row.uid,row.hostname,row.ip,row.port,row.b_id,row.b.id,row.b.caption)
return HttpResponse('host')
9.后台输出:
1 c1.com 1.1.1.1 9900 1 1 运维
2 c2.com 1.1.1.2 8800 1 1 运维
3 c3.com 1.1.1.3 7700 2 2 销售
10. <td>{{ forloop.counter}}</td> 可用于添加序号
第三课 创建多对多以及增加示例
1.创建多对多:
方式一:自定义关系表
class Host(models.Model):
uid=models.AutoField(primary_key=True)
hostname=models.CharField(max_length=32,db_index=True)
ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)
port=models.IntegerField()
b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)
class Application(models.Model):
name=models.CharField(max_length=32)
class HostToApplication(models.Model):
hobj=models.ForeignKey(to='Host',to_field='uid',on_delete=models.CASCADE)
aobj=models.ForeignKey(to='Application',to_field='id',on_delete=models.CASCADE)
方式二:自动创建关系表:
class Host(models.Model):
uid=models.AutoField(primary_key=True)
hostname=models.CharField(max_length=32,db_index=True)
ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)
port=models.IntegerField()
b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)
class Application(models.Model):
name=models.CharField(max_length=32)
r=models.ManyToManyField('Host')
#无法直接对第三张表进行操作 obj.r.all()
第二十章 Django数据库实战第一课 获取单表单数据的三种方式:urls.py中的路由代码:path('busniess',views.busniess),views.py中代码:def busniess(req):v1=models.busniess.objects.all()print('v1:',v1)#是对象v2=models.busniess.objects.all().values('id','caption')print('v2:',v2)#是字典v3=models.busniess.objects.values_list('id','caption')print('v3',v3)return render(req,'busniess.html',{'v1':v1,'v2':v2,'v3':v3})Template目录下创建busniess.html文件,代码:<ul>{% for i in v1 %}<li>{{ i.id }}-{{ i.caption }}-{{ i.code }}</li>{% endfor %}</ul><ul>{% for i in v2 %}<li>{{ i.id }}-{{ i.caption }}</li>{% endfor %}</ul><ul>{% for i in v3 %}<li>{{ i.0 }}-{{ i.1 }}</li>{% endfor %}</ul>浏览器访问URL:http://127.0.0.1:8000/app02/busniess首页显示:1-运维-aa2-销售-bb3-开发-cc4-渠道-dd1-运维2-销售3-开发4-渠道1-运维2-销售3-开发4-渠道运行输出:v1: <QuerySet [<busniess: busniess object (1)>, ...]>v2: <QuerySet [{'id': 1, 'caption': '运维'},...]>v3 <QuerySet [(1, '运维'), (2, '销售'), (3, '开发'), (4, '渠道')]>第二课 一对多跨表操作获取一对多表单数据:1.建立两张表,一张主键表busniess,一张参照表host,Models.py中代码:class busniess(models.Model):caption=models.CharField(max_length=64)code=models.CharField(max_length=32)class Host(models.Model):uid=models.AutoField(primary_key=True)hostname=models.CharField(max_length=32,db_index=True)ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)port=models.IntegerField()b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)2.在命令行下执行创建完毕:python manage.py makemigrationspython manage.py migrate3.在两张表中自定义一些数据。4.在urls.py中加入路径:path('host',views.host),5.在views.py中代码:def host(req):v1=models.Host.objects.filter(uid__gt=0)for row in v1:print(row.uid,row.hostname,row.ip,row.port,row.b_id,row.b)return HttpResponse('host')6.浏览器访问:http://127.0.0.1:8000/app02/host 触发函数7.后台输出:1 c1.com 1.1.1.1 9900 1 busniess object (1)2 c2.com 1.1.1.2 8800 1 busniess object (1)3 c3.com 1.1.1.3 7700 2 busniess object (2)8.修改5中代码:def host(req):v1=models.Host.objects.filter(uid__gt=0)for row in v1:print(row.uid,row.hostname,row.ip,row.port,row.b_id,row.b.id,row.b.caption)return HttpResponse('host')9.后台输出:1 c1.com 1.1.1.1 9900 1 1 运维2 c2.com 1.1.1.2 8800 1 1 运维3 c3.com 1.1.1.3 7700 2 2 销售10. <td>{{ forloop.counter}}</td> 可用于添加序号
第三课 创建多对多以及增加示例1.创建多对多:方式一:自定义关系表class Host(models.Model):uid=models.AutoField(primary_key=True)hostname=models.CharField(max_length=32,db_index=True)ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)port=models.IntegerField()b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)class Application(models.Model):name=models.CharField(max_length=32)class HostToApplication(models.Model):hobj=models.ForeignKey(to='Host',to_field='uid',on_delete=models.CASCADE)aobj=models.ForeignKey(to='Application',to_field='id',on_delete=models.CASCADE)方式二:自动创建关系表:class Host(models.Model):uid=models.AutoField(primary_key=True)hostname=models.CharField(max_length=32,db_index=True)ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)port=models.IntegerField()b=models.ForeignKey(to='busniess',to_field='id',on_delete=models.CASCADE)class Application(models.Model):name=models.CharField(max_length=32)r=models.ManyToManyField('Host')#无法直接对第三张表进行操作 obj.r.all()