1. models
from django.db import models
class Test(models.Model):
id = models.BigAutoField(primary_key=True, max_length=20)
name = models.CharField(max_length=255, null=False, blank=False)
value = models.TextField(db_column='value',null=False, blank=False)
def __str__(self):
reutrn self.name
class Meta:
db_table = 'test' #数据库名
2. serializers
from rest_framework import serializers
class TestSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField(read_only=True)
value = serializers.CharField(read_only=True)
3. views
from .models import Test
from .serializers import TestSerializer
from rest_framework.viewsets import GenericViewSet
from rest_framework import status
from django.http import HttpResponse, JsonResponse
from rest_framework.response import Response
class TestViewSet(GenericViewSet):
authentication_classes = (TokenAuthentication,)
serializer_class = CfsTestSerializer
queryset = CfsTest.objects.all()
def get_test(self, request, *args, **kwargs):
serializer = self.get_serializer(self.queryset)
print('\n==================\n')
print("data:")
print(serializer.data)
print("\n=================\n")
t0 = time.clock()
d = Test.objects.all()
serializer = TestSerializer(data=d, many=True)
serializer.is_valid()
datas = serializer.data
data = datas[0].get('value')
t1 = time.clock()
print("time:", t1-t0)
return Response("end...")
4. key_words
关键字:
-
db_column
-
db_table
-
null=True/False
-
blank=True/False
-
default='default'
-
primary_key=True
-
max_length=255
来源:CSDN
作者:Neil_001
链接:https://blog.csdn.net/Neil_001/article/details/103791452