[Efficiency Platform] Built-in function management module – get built-in function list data, view built-in function detailed data (8)

Hits: 0

1. Platform page display

1. List of [built-in functions]

2. Data display of built-in function details

Second, the model class definition

from django.db import models

# Create your models here.
from django.db import models

from utils.base_models import BaseModel


class  DebugTalks (BaseModel) : 
    name = models.CharField( 'debugtalk file name' , max_length= 200 , default= 'debugtalk.py' , help_text= 'debugtalk file name' )
    project = models.OneToOneField('projects.Projects', on_delete=models.CASCADE,
                                   related_name = 'debugtalks' , help_text= 'owned project' )

    class Meta:
        db_table = 'tb_debugtalks'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return self.name

3. [Serializer] Definition

According to the list page: display id, project name, built-in function name, and do not need to input;
debugtalk does not need to display, but needs to be input.

class  DebugtalksModelSerializer (serializers.ModelSerializer) : 
    project=serializers.StringRelatedField(label= 'Project name' ,help_text= 'Project name' ,read_only= True )

    class Meta:
        model=Debugtalks
        fields=['id','name','project','debugtalk']
        extra_kwargs={
            'debugtalk':{
                'write_only':True   
            }
        }

class DebugtalkSerializer(serializers.ModelSerializer):
    class Meta:
        model=Debugtalks
        fields=['id','debugtalk']

Fourth, the view class definition

hint:

1. This module does not involve adding built-in functions and deleting built-in functions, so there is no need to inherit ModelViewSet
2. ModelViewSet class inherits ListModelMixin, UpdateModelMixin, RetrieveModelMixin, CreateModelMixin, DestroyModelMixin
3. GenericViewSet inherits ViewSetMixin, GenericAPIView
4, inherits ViewSetMixin, Has the function of automatically generating a routing mechanism

class DebugtalksViewSet(ListModelMixin,
                    RetrieveModelMixin,
                    UpdateModelMixin,
                    GenericViewSet):
    queryset=Debugtalks.objects.all()
    serializer_class=DebugtalksModelSerializer
    pagination_class=PageNumberPagination
    permission_classes = [permissions.IsAuthenticated]

    def get_serializer_class(self):
        if self.action=='retrieve':
            return DebugtalkSerializer
        else:
            return super().get_serializer_class

Five, routing definition

router=routers.SimpleRouter()
router.register(r'debugtalk',DebugtalksViewSet)
urlpatterns=[
    path('',include(router.urls))
]

You may also like...

Leave a Reply

Your email address will not be published.