Django 地址接口开发

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

python 复制代码
python ../manage.py startapp address

继承了mixins扩展类,进到里面可以稍微看下源码

该方法帮我们实现了获取验证及保存的功能

address/views

python 复制代码
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

python 复制代码
from django.urls import path
from .views import AddressListGenericAPIView

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

address/serializers.py

python 复制代码
from rest_framework import serializers

from apps.address.models import UserAddress


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

address/models

python 复制代码
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'

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

获取地址

进入mix可以看到该类,retrieve方法通过传入的参数帮我们获取对应的数据

address/view

python 复制代码
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

python 复制代码
re_path("(?P<pk>.*)",AddressGenericAPIView.as_view()),

更新和删除

address/views

python 复制代码
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)

获取所有的地址信息

address/views

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

address/urls

python 复制代码
urlpatterns = [
    path("", AddressGenericAPIView.as_view()),
    path("list", AddressListGenericAPIView.as_view()),
    re_path("(?P<pk>.*)", AddressGenericAPIView.as_view()),

]

梳理 Mixin 混合类关系图

逐个继承,直到GenericAPIView 多了三个属性queryset,serializer_class,lookup_field,基于这几个属性可以对数据进行不同的操作

相关推荐
哇咔咔哇咔9 分钟前
【科普】conda、virtualenv, venv分别是什么?它们之间有什么区别?
python·conda·virtualenv
infiniteWei10 分钟前
【Lucene】什么是全文检索?解读结构化数据与非结构化数据
django·全文检索·lucene
怀旧66627 分钟前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
CSXB9931 分钟前
三十四、Python基础语法(文件操作-上)
开发语言·python·功能测试·测试工具
这该死迷人的编程1 小时前
区块链:Raft协议
区块链·1024程序员节
亚图跨际1 小时前
MATLAB和Python及R潜变量模型和降维
python·matlab·r语言·生物学·潜变量模型
infiniteWei1 小时前
【Lucene】全文检索 vs 顺序扫描,为何建立索引比逐个文件搜索更高效?
django·全文检索·lucene
IT古董1 小时前
【机器学习】决定系数(R²:Coefficient of Determination)
人工智能·python·机器学习
德育处主任Pro2 小时前
『Django』APIView基于类的用法
后端·python·django
Star Patrick2 小时前
算法训练(leetcode)二刷第十九天 | *39. 组合总和、*40. 组合总和 II、*131. 分割回文串
python·算法·leetcode