码迷,mamicode.com
首页 > 其他好文 > 详细

Django基础--3

时间:2018-12-28 13:34:05      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:hid   border   path   request   ble   orm   import   mod   dep   

ORM

django一对多的操作

获取表单的三种方式:views

获取对象            .all()      html中获取方式为 row.id,row,host

获取字典            .all().values("字段","字段","字段",……)  row.id,row,host

获取元祖            .all().values_list("字段","字段","字段",……) row.0,row.1

技术分享图片
#views

from django.shortcuts import render
from app import models
# Create your views here.
def home(request):
    v1 = models.Host.objects.all()
    print(v1)
    #<QuerySet [<Host: Host object (1)>, <Host: Host object (2)>, <Host: Host object (3)>]> 对象
    v2 = models.Host.objects.all().values("host","port")
    print(v2)
    #< QuerySet[{‘host‘: ‘a‘, ‘port‘: 80}, {‘host‘: ‘b‘, ‘port‘: 81}, {‘host‘: ‘c‘, ‘port‘: 82}] >  字典类型
    v3 = models.Host.objects.all().values_list("host","port")
    print(v3)
    #<QuerySet [(‘a‘, 80), (‘b‘, 81), (‘c‘, 82)]>  元祖类型
    retur

#html中

<body>
    <h1>主机信息</h1>
    <ul>
        {% for row in v1 %}
            <li>{{ row.id }}--- {{ row.host }}--- {{ row.ip }}--- {{ row.port }}--- {{ row.dept_id }}</li>
        {% endfor %}
    </ul>

    <h1>主机信息</h1>
    <ul>
        {% for row in v2 %}
            <li>{{ row.host }}--- {{ row.port }}</li>
        {% endfor %}
    </ul>

    <h1>主机信息</h1>
    <ul>
        {% for row in v3 %}
            <li>{{ row.0 }}--- {{ row.1 }}</li>
        {% endfor %}
    </ul>
</body>
View Code

一对多的跨表操作

获取对象 filter(id__gte=1)

 

技术分享图片
#views

def host(request):
    v1 = models.Host.objects.filter(id__gte=1)
    return render(request,"host.html",{"v1":v1})


#urls.py


from app import views
path(host/, views.host),




#host.html
<body>
    <table border="1">
        <thead>
            <tr>
                <th>id</th>
                <th>主机名</th>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>业务线</th>
                <th>部门</th>
                <th>部门代码</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v1 %}
                <tr>
                    <td>{{ row.id }}</td>
                    <td>{{ row.host }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept_id }}</td>
                    <td>{{ row.dept.dept }}</td>
                    <td>{{ row.dept.dept_en }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
</body>
View Code

 

以上将所有的数据都显示出来了,然而实际中并用不到这么多,这里可以隐藏两个表的id,因每次都会循环tr,直接在<tr>中定义两个属性放id

这里修改下tbody里面内容即可

<tbody>
            {% for row in v1 %}
                <tr hid = "{{ row.id }}" bid = "{{ row.dept.id }}">
                    <td>{{ row.host }}</td>
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept.dept }}</td>
                </tr>
            {% endfor %}
        </tbody>

获取字典 filter(id__gte=1).values("id","ip", "port", "dept_id","dept__dept")

 

#views.py

#获取类型为字典
    v2 = models.Host.objects.filter(id__gte=1).values("id","ip", "port", "dept_id","dept__dept")
    #print(v2)
    # <QuerySet [{‘ip‘: ‘1.1.1.1‘, ‘host‘: ‘a‘, ‘dept__dept‘: ‘运维‘}, {‘ip‘: ‘1.1.1.2‘, ‘host‘: ‘b‘, ‘dept__dept‘: ‘销售‘}, {‘ip‘: ‘1.1.1.3‘, ‘host‘: ‘c‘, ‘dept__dept‘: ‘信息部‘}]>
    for i in v2:
        print(i["id"],i["ip"],i["port"],i["dept__dept"])
  return render(request,"host.html",{"v2":v2})

 

前端操作:

<h2>业务线-字典</h2>
    <table border="1">
        <thead>
            <tr>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>部门</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v2 %}
                <tr hid = "{{ row.id }}" bid = "{{ row.dept_id }}">
                    <td>{{ row.ip }}</td>
                    <td>{{ row.port }}</td>
                    <td>{{ row.dept__dept }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

获取元祖  filter(id__gte=1).values_list("id","ip", "port", "dept_id","dept__dept")

 

v3 =  models.Host.objects.filter(id__gte=1).values_list("id","ip", "port", "dept_id","dept__dept")
    for j in v3:
        print(j[0],j[1],j[2],j[3],j[4],)
    return render(request,"host.html",{"v3":v3})

 

<h2>业务线-元祖</h2>
    <table border="1">
        <thead>
            <tr>
                <th>主机ip</th>
                <th>主机端口</th>
                <th>部门</th>
            </tr>
        </thead>
        <tbody>
            {% for row in v3 %}
                <tr hid = "{{ row.0 }}" bid = "{{ row.3 }}">
                    <td>{{ row.1 }}</td>
                    <td>{{ row.2 }}</td>
                    <td>{{ row.4}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

 

Django基础--3

标签:hid   border   path   request   ble   orm   import   mod   dep   

原文地址:https://www.cnblogs.com/Aline2/p/10189863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!