注意:git
# 注意:之後全部的數據刪除,儘可能用軟刪除,使用一個字段標誌是否刪除,而不是真正的從數據庫中刪除 -好處:1 這樣刪除數據不會影響索引,不會致使索引失效 2 以前存的用戶數據還在,以備之後使用
models.py--->數據遷移數據庫
# 抽象出一個基表(再也不數據庫生成,abstract=True),只用來繼承 class BaseModel(models.Model): is_delete = models.BooleanField(default=False) create_time = models.DateTimeField(auto_now_add=True) class Meta: # 基表必須設置abstract,基表就是給普通Model類繼承使用的,設置了abstract就不會完成數據庫遷移完成建表 abstract = True class Book(BaseModel): name = models.CharField(max_length=16) price = models.DecimalField(max_digits=5, decimal_places=2) publish = models.ForeignKey(to='Publish', db_constraint=False, on_delete=models.DO_NOTHING) # 重點:多對多外鍵實際在關係表中,ORM默認關係表中兩個外鍵都是級聯 # ManyToManyField字段不提供設置on_delete,若是想設置關係表級聯,只能手動定義關係表 authors = models.ManyToManyField(to='Author', related_name='books', db_constraint=False) @property def publish_name(self): return self.publish.name @property def author_list(self): # ll=[] # for author in self.authors.all(): # ll.append({'name':author.name,'sex':author.get_sex_display()}) return [{'name': author.name, 'sex': author.get_sex_display()} for author in self.authors.all()] class Publish(BaseModel): name = models.CharField(max_length=16) address = models.CharField(max_length=64) class Author(BaseModel): name = models.CharField(max_length=16) sex = models.IntegerField(choices=[(0, '男'), (1, '女')], default=0) class AuthorDetail(BaseModel): mobile = models.CharField(max_length=11) # 有做者能夠沒有詳情,刪除做者,詳情必定會被級聯刪除 # 外鍵字段爲正向查詢字段,related_name是反向查詢字段 author = models.OneToOneField(to='Author', related_name='detail', db_constraint=False, on_delete=models.CASCADE)
admin.pydjango
from django.contrib import admin from app01 import models admin.site.register(models.Book) admin.site.register(models.Publish) admin.site.register(models.Author) admin.site.register(models.AuthorDetail)
建立一個超級用戶,登陸admin後臺管理,造出一些數據(books ,authors,publish)app
serializer.pypost
# 圖書表序列化類 from app01 import models from rest_framework import serializers class ListBookSerializer(serializers.ListSerializer): # def create(self, validated_data): # print('=======',validated_data) # return '1' def update(self, instance, validated_data): print(instance) # book_list:是一堆圖書對象 print(validated_data) # 列表套字典,是要修改的數據 return [self.child.update(book, validated_data[i]) for i, book in enumerate(instance)] class BookSerializer(serializers.ModelSerializer): class Meta: model = models.Book list_serializer_class=ListBookSerializer # 指定many=True的時候,生成的ListBookSerializer的對象了 fields = ['name', 'price', 'publish', 'authors', 'publish_name', 'author_list'] extra_kwargs = { 'publish': {'write_only': True}, 'authors': {'write_only': True}, 'publish_name': {'read_only': True}, 'author_list': {'read_only': True}, } # def create(self, validated_data): # print(validated_data)
utils.pyurl
from rest_framework.response import Response class APIResponse(Response): def __init__(self, code=100, msg='成功', data=None, status=None, headers=None, content_type=None, **kwargs): dic = {'code': code, 'msg': msg} if data: dic['data'] = data dic.update(kwargs) super().__init__(data=dic, status=status, headers=headers, content_type=content_type) from rest_framework.views import exception_handler def common_exception(exc, context): # 先調用REST framework默認的異常處理方法得到標準錯誤響應對象 response = exception_handler(exc, context) # 在此處補充自定義的異常處理 if response is None: response = Response(data={'code':999,'msg':str(exc)}) return response
settings.pyspa
INSTALLED_APPS = [ ... 'rest_framework' ] REST_FRAMEWORK = { # 配置全局異常 'EXCEPTION_HANDLER': 'app01.utils.common_exception' }
views.pyrest
# book表單增羣增 from app01 import serializer from app01 import models from rest_framework.views import APIView from app01.utils import APIResponse class BookView(APIView): def post(self, request, *args, **kwargs): if isinstance(request.data, dict): # 增一條 ser = serializer.BookSerializer(data=request.data) ser.is_valid(raise_exception=True) ser.save() return APIResponse(data=ser.data) elif isinstance(request.data, list): # 增多條(讓many=True就能夠了) ser = serializer.BookSerializer(data=request.data, many=True) # 內部如何實現的? # many=True,ser不是BookSerializer對象,而是ListSerializer對象,套了一個個的BookSerializer print(type(ser)) ser.is_valid(raise_exception=True) # from rest_framework.serializers import ListSerializer ser.save() # ListSerializer的save return APIResponse(msg='增長%s條成功' % len(request.data)) def get(self, request, *args, **kwargs): pk = kwargs.get('pk', None) if pk: # 單查 # 方式一 # book=models.Book.objects.filter(id=pk).filter(is_delete=False).first() # if not book: # raise Exception('要查詢的不存在') # 方式二 book = models.Book.objects.get(id=pk, is_delete=False) ser = serializer.BookSerializer(instance=book) else: # 查全部 book_list = models.Book.objects.all().filter(is_delete=False) ser = serializer.BookSerializer(instance=book_list, many=True) return APIResponse(data=ser.data) def delete(self, request, *args, **kwargs): pk = kwargs.get('pk', None) pks = [] if pk: # 單條刪除 # res=models.Book.objects.filter(id=pk).update(is_delete=True) # print(res) # return APIResponse(msg='刪除成功') pks.append(pk) else: pks = request.data res = models.Book.objects.filter(id__in=pks).update(is_delete=True) if res >= 1: return APIResponse(msg='刪除%s條成功' % res) else: # raise Exception('沒有要刪除的數據') return APIResponse(code=999, msg='沒有要刪除的數據') def put(self, request, *args, **kwargs): pk = kwargs.get('pk', None) if pk: # 單條修改 book = models.Book.objects.get(id=pk, is_delete=False) ser = serializer.BookSerializer(instance=book, data=request.data) ser.is_valid(raise_exception=True) ser.save() return APIResponse(msg='修改爲功') else: # 分析:ListSerializer的update方法沒有寫,須要咱們本身寫 from rest_framework.serializers import ListSerializer # pks=[item['id'] for item in request.data] # 若是不重寫ListSerializer的update方法,這是存不進去的 pks = [] for item in request.data: pks.append(item['id']) item.pop('id') print(request.data) book_list = models.Book.objects.filter(id__in=pks, is_delete=False) ser = serializer.BookSerializer(instance=book_list, data=request.data, many=True) print(type(ser)) ser.is_valid(raise_exception=True) ser.save() return APIResponse(msg='修改%s條成功')
urls.pycode
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ ### book表 path('books/', views.BookView.as_view()), re_path('books/(?P<pk>\d+)', views.BookView.as_view()), ]