Django 地址接口开发

应用 Mixin 混合类进行收货地址接口开发

python ../manage.py startapp address

继承了mixins扩展类,进到里面可以稍微看下源码
Django 地址接口开发_第1张图片

该方法帮我们实现了获取验证及保存的功能
Django 地址接口开发_第2张图片

address/views

from rest_framework.generics import GenericAPIView
from rest_framework.mixins import CreateModelMixin

from apps.address.models import UserAddress
from apps.address.serializers import AddressSerializer
from apps.order.serializers import OrderGoodsSerializer


class AddressGenericAPIView(GenericAPIView,CreateModelMixin):
    queryset = UserAddress.objects
    serializer_class = AddressSerializer
    def post(self,request):
        return self.create(request)

address/urls

from django.urls import path
from .views import AddressListGenericAPIView

urlpatterns = [
    path("",AddressGenericAPIView.as_view()),
]

address/serializers.py

from rest_framework import serializers

from apps.address.models import UserAddress


class AddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserAddress
        fields = "__all__"

address/models

from django.db import models


class UserAddress(models.Model):
    email = models.CharField(max_length=255, blank=True, null=True)
    signer_name = models.CharField(max_length=255, blank=True, null=True)
    telphone = models.CharField(max_length=255, blank=True, null=True)
    signer_address = models.CharField(max_length=255, blank=True, null=True)
    district = models.CharField(max_length=255, blank=True, null=True)
    default = models.IntegerField(blank=True, null=True)
    # create_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'user_address'

Django 地址接口开发_第3张图片

应用 Mixin 混合类进行收货地址接口开发(二)

获取地址

进入mix可以看到该类,retrieve方法通过传入的参数帮我们获取对应的数据
Django 地址接口开发_第4张图片

address/view

class AddressGenericAPIView(GenericAPIView,CreateModelMixin,RetrieveModelMixin):
    queryset = UserAddress.objects
    serializer_class = AddressSerializer

	# lookup_field字段 这里没有写 因为默认就是pk
    def get(self,request,pk):
        return self.retrieve(request,pk)

address/urls

re_path("(?P.*)",AddressGenericAPIView.as_view()),

Django 地址接口开发_第5张图片

更新和删除
address/views

class AddressGenericAPIView(GenericAPIView,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin):
    queryset = UserAddress.objects
    serializer_class = AddressSerializer
    def post(self,request):
        return self.create(request)

    def get(self,request,pk):
        return self.retrieve(request,pk)

    def put(self,request,pk):
        return self.update(request,pk)

    def delete(self,request,pk):
        return self.destroy(request,pk)

Django 地址接口开发_第6张图片

获取所有的地址信息

address/views

class AddressListGenericAPIView(GenericAPIView,ListModelMixin):
    queryset = UserAddress.objects
    serializer_class = AddressSerializer
    def get(self,request):
        return self.list(request)

address/urls

urlpatterns = [
    path("", AddressGenericAPIView.as_view()),
    path("list", AddressListGenericAPIView.as_view()),
    re_path("(?P.*)", AddressGenericAPIView.as_view()),

]

梳理 Mixin 混合类关系图

逐个继承,直到GenericAPIView 多了三个属性queryset,serializer_class,lookup_field,基于这几个属性可以对数据进行不同的操作
Django 地址接口开发_第7张图片

你可能感兴趣的:(Django,1024程序员节,django,python)