Output Django queryset as JSON

后端 未结 6 1713
陌清茗
陌清茗 2020-11-29 01:20

I want to serialize my queryset, and I want it in a format as this view outputs:

class JSONListView(ListView):
    queryset = Users.objects.all()

    def ge         


        
6条回答
  •  执念已碎
    2020-11-29 01:56

    If the goal is to build an API that allow you to access your models in JSON format I recommend you to use the django-restframework that is an enormously popular package within the Django community to achieve this type of tasks.

    • Django Rest Framework Website
    • Github

    It include useful features such as Pagination, Defining Serializers, Nested models/relations and more. Even if you only want to do minor Javascript tasks and Ajax calls I would still suggest you to build a proper API using the Django Rest Framework instead of manually defining the JSON response.

提交回复
热议问题