您好,登錄后才能下訂單哦!
這篇文章主要介紹python Django如何實(shí)現(xiàn)增刪改查,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
python .\manage.py startapp app01
然后創(chuàng)建數(shù)據(jù)模型
app01下的
modules.py文件
from django.db import models # Create your models here. class UserInfo(models.Model): name=models.CharField(max_length=32) password=models.CharField(max_length=12) age=models.IntegerField() tel=models.IntegerField(max_length=11)
配置數(shù)據(jù)庫
'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': '數(shù)據(jù)庫名', 'USER': '數(shù)據(jù)庫用戶名', 'PASSWORD': '密碼', 'HOST': '127.0.0.1', 'PORT': '3306', }
python manage.py makemigrations 和 python manage.py migrate
from django.contrib import admin from django.urls import path,include from app01 import views urlpatterns = [ # path('admin/', admin.site.urls), # path('index/',views.index), # path('user/list/', views.user_list), # path('login/', views.ApiLogin.as_view()), # path('del/', views.ApiDel.as_view()), path('add/', views.addUser.as_view()), path('user/', views.UserList.as_view()), path('updateuser/', views.upDateUser.as_view()), path('del/', views.deluser.as_view()), ]
from django.shortcuts import render,HttpResponse from rest_framework.views import APIView # Create your views here. from django import views from app01.models import UserInfo from rest_framework.response import Response from app01.ser import APIViewUserInfo class addUser(APIView): def post(self,request): obj=APIViewUserInfo(data=request.data) if obj.is_valid(): obj.save() return Response({"data":obj.data, "status" : 201,"message":"添加用戶成功"}) return Response(data=obj.errors,status=400) class UserList(APIView): def get(self,request): obj=UserInfo.objects.all() ser=APIViewUserInfo(instance=obj,many=True) return Response(ser.data) class upDateUser(APIView): def post(self,request): print(request.data.get('id')) i=request.data.get('id') try: user=UserInfo.objects.get(id=i) except Exception as e: return Response(data='不存在', status=201) # 創(chuàng)建序列化對象,并將要反序列化的數(shù)據(jù)傳遞給data構(gòu)造參數(shù),進(jìn)而進(jìn)行驗(yàn)證 user.password=request.data.get('password') if request.data.get('name')!='': print(request.data.get('name')=='') user.name=request.data.get('name') user.save() return Response(status=400) class deluser(APIView): def post(self,request): id=request.data.get('id') UserInfo.objects.filter(id=id).delete() return Response({"msg":'刪除成功',"state":"true"}) # def index(request): # return HttpResponse("歡迎使用") # # # def user_list(request): # return render(request,"user_list.html") # # class ApiLogin(APIView): # def get(self,request): # return HttpResponse('get') # def post(self,request): # UserInfo.objects.create(name='張三',password='123456',age=15,tel='1234567891') # obj=UserInfo.objects.all() # print(obj) # return HttpResponse('post') # # class ApiDel(APIView): # def post(self,request): # UserInfo.objects.filter(id=4).delete() # return HttpResponse('刪除成功')
# -*- coding: utf-8 -*- from rest_framework import serializers from app01.models import UserInfo class APIViewUserInfo(serializers.Serializer): """圖書數(shù)據(jù)序列化器""" id = serializers.IntegerField(label='ID', read_only=True) # 主鍵序列化 # 第一:普通字段序列化 name = serializers.CharField(label='名稱', max_length=20) password = serializers.CharField(label='密碼') age = serializers.IntegerField(label='年齡', required=False) tel = serializers.IntegerField(label='電話', required=False) # # 第二:一對多字段序列化 # heroinfo_set = serializers.PrimaryKeyRelatedField(read_only=True, many=True) # # 第三:自定義顯示(顯示多對多) # xxx = serializers.SerializerMethodField(read_only=True) class Meta: model = UserInfo # 自定義顯示 多對多 字段 # 定義創(chuàng)建語法:ser.save()執(zhí)行,就會立刻調(diào)用create方法用來創(chuàng)建數(shù)據(jù) def create(self, validated_data): '''validated_data: 表單或者vue請求攜帶的json:{"username":"zhangsan","password":"123456"}''' return self.Meta.model.objects.create(**validated_data)
以上是“python Django如何實(shí)現(xiàn)增刪改查”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。