django获取单表数据的三种方式
v1 = models.Business.objects.all()
# v1是QuerySet的列表 ,内部元素都是对象
v2 = models.Business.objects.all().values('id','caption') //这里不加all()也可以
# v2是QuerySet的列表 ,内部元素都是字典
v3 = models.Business.objects.all().values_list('id','caption') //这里不加all()也可以
# v3是QuerySet的列表 ,内部元素都是元组
def business(request): v1 = models.Business.objects.all() v2 = models.Business.objects.all().values('id','caption') v3 = models.Business.objects.all().values_list('id','caption') print(v1,v2,v3) return render(request, 'business.html', {'v1': v1, 'v2': v2, 'v3': v3})
business.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <h1>业务线列表(对象)</h1> <ul> {% for row in v1 %} <li>{{ row.id }} - {{ row.caption }} - {{ row.code }}</li> {% endfor %} </ul> <h1>业务线列表(字典)</h1> <ul> {% for row in v2 %} <li>{{ row.id }} - {{ row.caption }}</li> {% endfor %} </ul> <h1>业务线列表(元组)</h1> <ul> {% for row in v3 %} <li>{{ row.0 }} - {{ row.1 }}</li> {% endfor %} </ul> </body> </html>
获取一个对象
方法一:如果存在就返回一个对象,如果不存在就报错,不推荐
models.Business.objects.get(id=1)
方法二:如果存在就返回一个对象,不存在就返回none,推荐
models.Business.objects.filter(id=1).first()
外键:
class Business(models.Model): caption = models.CharField(max_length=32) code = models.CharField(max_length=32,null=True,default='SA') class Host(models.Model): id = models.AutoField(primary_key=True) hostname = models.CharField(max_length=32) ip = models.GenericIPAddressField(protocol='ipv4') port = models.IntegerField() b = models.ForeignKey(to='Business',to_field='id')
v = models.Host.objects.filter(nid__gt=0)
v[0].b.caption ----> 通过点(.)进行跨表
实例:跨表查询的三种方式
def host(request): host_obj = models.Host.objects.all() host_obj2 = models.Host.objects.filter(b__caption='运维').values('id','hostname','ip','port','b__code') for i in host_obj2: print(i['id'],i['hostname'],i['ip'],i['port'],i['b__code']) host_obj3 = models.Host.objects.filter(b__caption='运维').values_list('id', 'hostname', 'ip', 'port', 'b__code') return render(request,'host.html',{'host_obj':host_obj,'host_obj2':host_obj2,'host_obj3':host_obj3})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>对象</h2> <table border="1px"> <thead> <tr> <th>ID</th> <th>主机名</th> <th>ip</th> <th>port</th> <th>业务线</th> <th>业务线编码</th> </tr> </thead> <tbody> {% for i in host_obj %} <tr hid="{{ i.id }}" bid="{{ i.b_id }}"> <td>{{ forloop.counter }}</td> <td>{{ i.hostname }}</td> <td>{{ i.ip }}</td> <td>{{ i.port }}</td> <td>{{ i.b.caption }}</td> <td>{{ i.b.code }}</td> </tr> {% endfor %} </tbody> </table> <h2>字典</h2> <table border="1px"> <thead> <tr> <th>ID</th> <th>主机名</th> <th>ip</th> <th>port</th> <th>业务线编码</th> </tr> </thead> <tbody> {% for i in host_obj2 %} <tr hid="{{ i.id }}" bid="{{ i.b_id }}"> <td>{{ forloop.counter }}</td> <td>{{ i.hostname }}</td> <td>{{ i.ip }}</td> <td>{{ i.port }}</td> <td>{{ i.b__code }}</td> </tr> {% endfor %} </tbody> </table> <h2>元组</h2> <table border="1px"> <thead> <tr> <th>ID</th> <th>主机名</th> <th>ip</th> <th>port</th> <th>业务线编码</th> </tr> </thead> <tbody> {% for i in host_obj3 %} <tr hid="{{ i.0 }}" bid="{{ i.b_id }}"> <td>{{ forloop.counter }}</td> <td>{{ i.1 }}</td> <td>{{ i.2 }}</td> <td>{{ i.3 }}</td> <td>{{ i.4 }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>