Django Rest Framework中的成功消息

时间:2019-02-19 08:11:12

标签: django django-models django-rest-framework django-views

我想发送成功message status result_count。重播响应时。类似于下面的JSON响应

{
"status":true,
"result_count":5,
"message":"Timesheet Retrieved Successfully.",
"result": [
{
    "user_id": "40",
    "user_name": "Derek Hedrick",
    "data": [
    {
        "timesheet_id": "2",
        "job_id": "11",
        "job_code": "rfyxlv",
        "clock_in": "2018-09-07T03:00",
        "clock_out": "2018-09-07T15:00"
        "note": "This is test note",
        "type":
        {
             "regular":"7",
             "overtime":"2"
         }
    },

但是我得到这样的答复

[
    {
        "id": 1,
        "job_group": 1,
        "job_name": "RAJJUUU",
        "job_number": "hjhasfd",
        "job_description": "Json",
        "job_start_date": "2018-03-29",
        "job_start_time": "14:20:00",
        "job_end_date": "2018-03-29",
        "job_end_time": "14:20:00",
        "job_status": "Active",
        "job_created_on": "2019-01-28T08:32:47.327096Z"
    }
]

如何添加自定义消息。这是我的ListAPIView

class JobsListAPIView(generics.ListAPIView):
    serializer_class = JobListSerialzer
    # pagination_class = ListLimitOffsetPagination
    permission_classes = [UserIsAuthenticated]

    def get_queryset(self, *args, **kwargs):
        # print('self.request.auth', self.request.auth.application.user.userprofile.user_company.id)

        qs = Jobs.objects.exclude(job_is_deleted = True).filter(
            job_company = self.request.auth.application.company
        )

        start_date = self.request.query_params.get('start_date', None)
        end_date = self.request.query_params.get('end_date', None)
        job_name = self.request.query_params.get('job_name', None)


        if start_date is not None:
            qs=qs.filter(job_start_date=start_date)
        if end_date is not None:
            qs=qs.filter(job_end_date=end_date)
        if job_name is not None:
            qs=qs.filter(job_name=job_name)

        return qs

Serializer.py

class JobListSerialzer(serializers.ModelSerializer):
    class Meta:
        model = Jobs
        fields = [
            'id',
            'job_group',
            'job_name', 
            'job_number',
            'job_description',
            'job_start_date',
            'job_start_time',
            'job_end_date',
            'job_end_time',
            'job_status',
            'job_created_on',
        ]

1 个答案:

答案 0 :(得分:2)

这将起作用:

class JobsListAPIView(generics.ListAPIView):
    serializer_class = JobListSerialzer
    permission_classes = [UserIsAuthenticated]

    def get(self,request, *args, **kwargs):
        if request.method == 'GET':

            qs = Jobs.objects.exclude(job_is_deleted = True).filter(
                job_company = self.request.auth.application.company
            )

            start_date = self.request.query_params.get('start_date', None)
            end_date = self.request.query_params.get('end_date', None)
            job_name = self.request.query_params.get('job_name', None)

            if start_date is not None:
                qs=qs.filter(job_start_date=start_date)
            if end_date is not None:
                qs=qs.filter(job_end_date=end_date)
            if job_name is not None:
                qs=qs.filter(job_name=job_name)
            ax = self.serializer_class(qs, many=True)
            return Response (
            {
                "message":"Jobs Retrieved Successfully.",
                "status" : True,
                "result": ax.data,
                "result_count" : qs.count()

                }
        )