1. 程式人生 > >Django中orm的簡述

Django中orm的簡述

一、orm的增刪查改

orm建立表如下:

class User(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=32)
    password = models.CharField(max_length=32)
    adder=models.CharField(max_length=64,default='上海')

查:可以使用(render(request,'user_list.html','lian_list':set))獲得

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主表</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>
password</th> <th>adder</th> </tr> </thead> <tbody> {% for lian in lian_list %} <tr> <td>{{ lian.id }}</td> <td>{{ lian.name }}</td> <td>{{ lian.password }}</td>
<td>{{ lian.adder }}</td> <td> <a href="/del_list/?id={{ lian.id }}">刪除</a></td> <td> <a href="/update_list/?id={{ lian.id }}">改變</a></td> </tr> {% endfor %} </tbody> <a href="/add_list/">新增</a> </table> </body> </html>
user_list主頁程式碼
def user_list(request):
    if request.method == 'GET':
        ret = models.User.objects.all()
        return render(request, 'user_list.html', {'lian_list': ret})
views中user_list程式碼

增:可以直接在models中建立的User表中新增一個欄位adder後執行Python3 manag.py makemigrations 與python3 manage.py migrate

有兩種方法分別為

1.models.User.objects.create(name=name,password=password,adder=adder)

2.res=models.User(name=name,password=password,adder=adder)

res.argv()

def update_list(request):
    if request.method=='GET':
        return render(request,'update_list.html')
    elif request.method=='POST':
        id = request.GET.get('id')
        name=request.POST.get('name')
        password=request.POST.get('password')
        adder=request.POST.get('adder')
        models.User.objects.filter(id=id).update(name=name,password=password,adder=adder)
        return redirect('/user_list/')
views中update_list程式碼

 

刪:可以分別執行以下程式碼

1.

在user_list.html中<tobody>下<tr>下<td> <a href="/del_list/?id={{ lian.id }}">刪除</a></td>
View Code

2.

def del_list(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        models.User.objects.filter(id=id).delete()
        return redirect('/user_list/')
views中del_list程式碼

改:方法為(models.User.objects.filter(id=id).update(name=name,password=password,adder=adder))

def add_list(request):
    if request.method == 'GET':
        return render(request, 'add_list.html')
    elif request.method == 'POST':
        name = request.POST.get('name')
        password = request.POST.get('password')
        adder = request.POST.get('adder')
        models.User.objects.create(name=name, password=password, adder=adder)
        return redirect('/user_list/')
views中add_list程式碼

二、user的增刪查改

***重點****:
1 單表查詢所有使用者:models.User.objects.all()
得到的是 queryset物件(當成列表),列表裡面,一個一個的物件[user1,user2]
2 render(request, 'userlist.html', {'user_list': ret})
3 模板裡: {% for user in user_list %}
#要迴圈的內容
{{user.name}}
{% endfor%}
4 get請求攜帶引數:
http://127.0.0.1:8000/deleteuser/?id=1
後臺取值:request.GET.get('id')
request.GET['id']
5 orm刪除記錄 models.User.objects.filter(id=id).delete()
返回值:影響的行數
6 前臺post提交的資料取值:name=request.POST.get('name')
7 orm儲存:
兩種方式:
1 user=models.User.objects.create(name=name,password=pwd,address=addr)
2 user=models.User(name=name,password=pwd,address=addr)
user.save()
8 orm查詢單條資料:user=models.User.objects.filter(id=id).first()
9 orm的修改 models.User.objects.filter(id=id).update(name=name,password=pwd,address=addr)

演示如下:

"""lianxi URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^user_list/', views.user_list),
    url(r'^del_list/',views.del_list),
    url(r'^add_list/',views.add_list),
    url(r'^update_list/', views.update_list),
]
urls路由
from django.shortcuts import render, redirect
from app01 import models


# Create your views here.
def user_list(request):
    if request.method == 'GET':
        ret = models.User.objects.all()
        return render(request, 'user_list.html', {'lian_list': ret})


def del_list(request):
    if request.method == 'GET':
        id = request.GET.get('id')
        models.User.objects.filter(id=id).delete()
        return redirect('/user_list/')


def add_list(request):
    if request.method == 'GET':
        return render(request, 'add_list.html')
    elif request.method == 'POST':
        name = request.POST.get('name')
        password = request.POST.get('password')
        adder = request.POST.get('adder')
        models.User.objects.create(name=name, password=password, adder=adder)
        return redirect('/user_list/')
def update_list(request):
    if request.method=='GET':
        return render(request,'update_list.html')
    elif request.method=='POST':
        id = request.GET.get('id')
        name=request.POST.get('name')
        password=request.POST.get('password')
        adder=request.POST.get('adder')
        models.User.objects.filter(id=id).update(name=name,password=password,adder=adder)
        return redirect('/user_list/')
views檢視函式
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主表</title>
</head>
<body>
<table border="1">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>password</th>
        <th>adder</th>
    </tr>
    </thead>
    <tbody>
    {% for lian in lian_list %}
        <tr>
            <td>{{ lian.id }}</td>
            <td>{{ lian.name }}</td>
            <td>{{ lian.password }}</td>
            <td>{{ lian.adder }}</td>
            <td> <a href="/del_list/?id={{ lian.id }}">刪除</a></td>
            <td> <a href="/update_list/?id={{ lian.id }}">改變</a></td>
        </tr>
    {% endfor %}

    </tbody>
    <a href="/add_list/">新增</a>
</table>
</body>
</html>
templates下user_list
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>改變</title>
</head>
<body>
<form action="" method="post">
    <p>使用者名稱: <input type="text" name="name"></p>
    <p>密碼: <input type="password" name="password"></p>
    <p>地址: <input type="text" name="adder"></p>
    <input type="submit" value="改變">
</form>
</body>
</html>
templates下update_list
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增</title>
</head>
<body>
<form action="" method="post">
    <p>使用者名稱: <input type="text" name="name"></p>
    <p>密碼: <input type="password" name="password"></p>
    <p>地址: <input type="text" name="adder"></p>
    <input type="submit" value="提交">
</form>
</body>
</html>
templates下add_list