DJANGO ListView returns HTTPResponse,但我需要一个 json
DJANGO ListView returns HTTPResponse, but I need a json
我在 Django 中遇到基于 Class 的视图的问题。我需要创建 JSON 响应,而不是渲染到模板。
class AllItem(ListView):
model = MyModel
context_object_name = 'items'
template_name = 'mymodel/index.html'
我也有序列化器class
class SpendingConfigSerializer(serializers.ModelSerialier):
class Meta:
model = SpendingConfig
fields = ('record_date', 'amount', 'name')
有人知道如何连接它们吗?
谢谢
Z.
您可以使用 ListAPIView
并将您的 SpendingConfigSerializer
指定为序列化程序:
from rest_framework import generics
class UserList(<strong>generics.ListCreateAPIView</strong>):
queryset = SpendingConfig.objects.all()
<strong>serializer_class = SpendingConfigSerializer</strong>
我在 Django 中遇到基于 Class 的视图的问题。我需要创建 JSON 响应,而不是渲染到模板。
class AllItem(ListView):
model = MyModel
context_object_name = 'items'
template_name = 'mymodel/index.html'
我也有序列化器class
class SpendingConfigSerializer(serializers.ModelSerialier):
class Meta:
model = SpendingConfig
fields = ('record_date', 'amount', 'name')
有人知道如何连接它们吗?
谢谢 Z.
您可以使用 ListAPIView
并将您的 SpendingConfigSerializer
指定为序列化程序:
from rest_framework import generics
class UserList(<strong>generics.ListCreateAPIView</strong>):
queryset = SpendingConfig.objects.all()
<strong>serializer_class = SpendingConfigSerializer</strong>