django获取表单数据

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>

 

转载于:https://www.cnblogs.com/xone/p/6692449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值