DRF + JWT部分图像更新和GET过滤

问题描述 投票:0回答:1

我为我的Django 'Time Management'应用提供了Rest API。它有两个模型,一个Employee模型和一个Attendance模型。

这是我的serializers.py外观:

class EmployeeSerializer(serializers.ModelSerializer):
    class Meta:
        model = Employee
        fields = ('employee_id', 'employee_code', 'employee_first_name', 'employee_last_name', 'image_front', 'image_side_one', 'image_side_two')


class AttendanceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Attendance

        #Fields:
        #employee (Foreign Key of Employee)
        #starttime (Datetime Field, required)
        #startimage (Image Field, requried)
        #endtime (Datetime Field, null)
        #endimage (Image Field, null)
        #date (Date Field, automatically added of the date of startime.)

        fields = "__all__"

问题1-我想检查一个employee_code是否存在。我是否获得所有雇员并遍历每个雇员if employee_code == Desired的所有字段?还是有一种在发送获取请求时进行过滤的方法?

这是我的views

class EmployeeView(viewsets.ReadOnlyModelViewSet):
    queryset = Employee.objects.all()
    serializer_class = EmployeeSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

class AttendanceView(viewsets.ModelViewSet):
    queryset = Attendance.objects.all()
    serializer_class = AttendanceSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)

    parser_classes = (MultiPartParser, FormParser)
    def post(self, request, *args, **kwargs):
        file_serializer = AttendanceSerializer(data=request.data)
        if file_serializer.is_valid():
            file_serializer.save()
            return Response(file_serializer.data, status=status.HTTP_201_CREATED)
        else:
            return Response(file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

问题2-我添加了多文件解析器。现在怎么办?我使用的不是POSTMAN,而是使用Python请求库通过客户端应用程序中的API通过API更新出勤表。基本上,这就是此表的填充方式。

[员工输入他们的employee_code,如果有效,则在出勤表上的新条目,其中starttimedatetime.datetime.now(),另外starttimage会是一张图片(我也不知道如何使用python请求库)。然后,一段时间后,同一名员工将回来,输入他们的代码并更新添加了starttime并添加了endtimeendimage的相同条目。我在这里遇到的问题是:

a)我不知道如何上传图像。b)如果它是一个简单的MYSQL数据库,我将执行以下查询:

SELECT starttime, endtime FROM attendance
    WHERE employee_id = (I first get the employee ID from the employee table for reasons unknown) ORDER BY starttime DESC, endtime LIMIT 1;

然后查看是否有结束时间,如果有,这意味着员工被挤出了时间,但是如果没有,则意味着他没有。假设该员工尚未退出。如何更新他们的最后一个条目。C)我真的很稠密,并以必要的方式扩展了这个简单的问题。对不起。

谢谢您的时间。我真的很感激。

django django-rest-framework django-rest-framework-jwt
1个回答
0
投票

好,所以我不会再赘述了。我知道以前已经回答过这个问题,但是如果万一有其他问题和我一样,这些是我所指的字段。

1-Filtering Against query parameter in this link2-Got the last attendance from Start Time as desecending3-By overriding the get_queryset method

© www.soinside.com 2019 - 2024. All rights reserved.