标签:信息 book objects 使用 sel cts name 相关 meta
一、准备工作:首先定义好model以及视图类相关信息
class Book(models.Model):
name = models.CharField(max_length=32)
pub_num = models.IntegerField(default=10)
salary_num = models.IntegerField(default=100)
def __str__(self):
return self.name
class CheckView(viewsets.GenericViewSet, mixins.ListModelMixin):
queryset = models.Book.objects.all()
serializer_class = BookSerializer
def get(self, request):
res = self.serializer_class(self.queryset, many=True)
return Response(res.data)
class BookSerializer(serializers.ModelSerializer):
authors_info = serializers.SerializerMethodField(read_only=True)
def get_authors_info(self, obj):
return [{‘id‘: author.id, ‘name‘: author.name} for author in obj.authors.all()]
class Meta:
fields = ‘__all__‘
model = models.Book
标签:信息 book objects 使用 sel cts name 相关 meta
原文地址:https://www.cnblogs.com/lz666lz/p/12681221.html