【一】轮播图接口
【1】创建表
from django.db import models
class BaseModel(models.Model):
created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
updated_time = models.DateTimeField(auto_now=True, verbose_name='最后更新时间')
is_delete = models.BooleanField(default=False, verbose_name='是否删除')
is_show = models.BooleanField(default=True, verbose_name='是否上架')
orders = models.IntegerField(verbose_name='优先级')
class Meta:
abstract = True # 只用来继承,不在数据库中生成表
from django.db import models
# Create your models here.
from django.db import models
from luffy.utils.common_model import BaseModel
class Banner(BaseModel):
title = models.CharField(max_length=16, unique=True, verbose_name='名称')
image = models.ImageField(upload_to='banner', verbose_name='图片')
# web , 小程序 ,app --》轮播图--》可以点击--》点击--》跳转到某个地址
# 跳转有两种情况:1 外链 2 自己的页面
link = models.CharField(max_length=64, verbose_name='跳转链接')
info = models.TextField(verbose_name='详情')
class Meta:
db_table='luffy_banner'
verbose_name = '轮播图表'
verbose_name_plural = verbose_name
def __str__(self):
return self.title
【2】BannerView
from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin
from luffy.apps.home.models import Banner
from luffy.apps.home.serializer import BannerSerializer
from utils.common_mixin import APIListModelMixin
# Create your views here.
# 查询所有轮播图接口
class BannerView(GenericViewSet, APIListModelMixin):
# 按优先级的顺序来查
queryset = Banner.objects.all().filter(is_delete=False,is_show=True).order_by("orders")[0:settings.BANNER_COUNT]
serializer_class = BannerSerializer
【3】序列化类
from rest_framework import serializers
from luffy.apps.home.models import Banner
class BannerSerializer(serializers.ModelSerializer):
class Meta:
model = Banner
fields = ['title', 'image', 'link', 'info']
【4】配置路由
from django.urls import path,include
from rest_framework.routers import SimpleRouter
from luffy.apps.home.views import BannerView
router=SimpleRouter()
router.register('banner',BannerView,basename='banner')
urlpatterns = [
path('',include(router.urls))
]
【5】定制返回格式(APIResponse)
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin, CreateModelMixin, UpdateModelMixin, \
DestroyModelMixin
from utils.common_response import APIResponse
class APIListModelMixin(ListModelMixin):
def list(self, request, *args, **kwargs):
res = super().list(request, *args, **kwargs)
return APIResponse(result=res.data)
![image-20240510195158159](https://img2023.cnblogs.com/blog/3331555/202405/3331555-20240510195205058-2024666905.png)
【二】自定义配置
- settings/common_settings.py
# 用户自己的配置,开发或线上都会用
BANNER_COUNT = 3
from .common_settings import *
【三】simpleui
【1】安装
pip install django-simpleui
【2】app注册
INSTALLED_APPS = [
'simpleui',
]
【3】在admin中注册
from django.contrib import admin
# Register your models here.
from .models import Banner
# Register your models here.
@admin.register(Banner)
class BannerAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'image', 'link')
from .models import User
admin.site.register(User)
【四】项目架构
项目架构
-1 纯大单体
-一个后端 django
-小程序 :一个项目
-后台管理 :一个项目
-主站:一个项目 或者 后台管理和主站是同一个响应
-2 微服务架构--》服务和服务间通信--》消息队列
-后台管理:vue 后台管理 +django +自己数据库
-主站: vue写主站 +go +自己数据库
-小程序:微信小程序 +java +自己数据库
-3 微服务架构
-前端淘宝:访问不同功能,地址不一样即可
-后端:go 订单板块,java商品板块,python 用户行为分析
【五】模型表做序列化
# 混入类,提供Model实例to_dict方法
class ModelMixin(object):
__slots__ = ()
def to_dict(self, excludes: tuple = None, selects: tuple = None) -> dict:
if not hasattr(self, '_meta'):
raise TypeError('<%r> does not a django.db.models.Model object.' % self)
elif selects:
return {f: getattr(self, f) for f in selects}
elif excludes:
return {f.attname: getattr(self, f.attname) for f in self._meta.fields if f.attname not in excludes}
else:
return {f.attname: getattr(self, f.attname) for f in self._meta.fields}
def update_by_dict(self, data):
for key, value in data.items():
setattr(self, key, value)
self.save()