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,基于这几个属性可以对数据进行不同的操作

相关推荐
游客52020 分钟前
设计模式-创建型-工厂方法模式
开发语言·python·设计模式·工厂方法模式
m0_7482398323 分钟前
Python毕业设计选题:基于django的民族服饰数据分析系统的设计与实现_hadoop+spider
python·django·课程设计
m0_7482349024 分钟前
Hmsc包开展群落数据联合物种分布模型分析通用流程(Pipelines)
开发语言·python
m0_7482455228 分钟前
Python大数据可视化:基于python大数据的电脑硬件推荐系统_flask+Hadoop+spider
大数据·python·flask
m0_7482370532 分钟前
Python毕业设计选题:基于python的酒店推荐系统_django+hadoop
python·django·课程设计
m0_748254091 小时前
100天精通Python(爬虫篇)——第113天:爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python
小爬虫程序猿1 小时前
深入理解Jsoup与Selenium:Java爬虫的双剑合璧
爬虫·python·selenium
随便写写1 小时前
Pyside6 基础框架以及三种基础控件
python
夏娃同学1 小时前
基于Flask后端框架的均值填充
python·flask
HackKong1 小时前
Python与黑客技术
网络·python·web安全·网络安全·php