luffy: How to create a database and interface

1 Create a Banner database

Since the time, deletion, etc. in this database can be shared, we can write a separate table model for him, and finally make it not built into the database when the database is built. This code can be shared, and then we inherit BaseModels

from django.db import models

class BaseModels(models.Model):
    created_time = models.DateTimeField(auto_now_add=True, verbose_name= ' created time ' )
    updated_time = models.DateTimeField(auto_now=True, verbose_name= ' last update time ' )
    is_delete = models.BooleanField(default=False, verbose_name= ' Delete or not ' )
    is_show = models.BooleanField(default=True, verbose_name= ' Whether it is on the shelf ' )
    orders = models.IntegerField(verbose_name= ' priority ' )

    class Meta:
        abstract = True
from utils.models import BaseModels
 from django.db import models

class Banner(BaseModels):
    title = models.CharField(max_length=16, unique=True, verbose_name= ' name ' )
    image = models.ImageField(upload_to= ' banner ' , verbose_name= ' image ' )
    link = models.CharField(max_length=64, verbose_name= ' jump link ' )
    info = models.TextField(verbose_name= ' details ' )

    class Meta:
        db_table = ' luffy_banner ' 
        verbose_name_plural = ' Carousel chart '

    def  __str__ (self):
         return self.title

At this point, our Banner table is created

2 Write the Banner interface

By inheriting GenericViewSet and ListModelMixin, we can quickly write the interface, but if we want to return data in a specified format, we must pass the list data to another variable from the list method in ListModelMixin, so that we can easily Pass the value according to the specified parameters of the function. Since this list method may be reused in many places, we can make a package for it.

from rest_framework.mixins import ListModelMixin
 from .response import APIResponse

class BannerListView(ListModelMixin):
     def list(self, request, *args, ** kwargs):
        res = super().list(request, *args, ** kwargs)
         return APIResponse(
from luffyapi.utils.listviews import BannerListView
 from .models import Banner
 from .serializer import BannerSerializers
 # Create your views here. 
from rest_framework.viewsets import GenericViewSet
 from django.conf import settings

class BannerView(GenericViewSet, BannerListView):
    queryset =Banner.objects.all().filter(is_delete=False,is_show=True).order_by( ' orders ' )[:settings.AREA]
    serializer_class =BannerSerializers

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *