如何使用update_or_create进行功能制作?

时间:2019-04-17 07:35:35

标签: python django

我要设置两个序列化器。 TimesheetEntrySerializerTimesheeSerializerTimesheetEntrySerializer也是内联的,并且在admin.py文件中进行内嵌。我需要帮助为update_or_create制作两个模型。 这是API

这是API

"results": [
        {
            "id": 46,
            "date": "2019-04-10",
            "user": {
                "pk": 4,
                "username": "Boris",
                "email": "",
                "first_name": "",
                "last_name": ""
            },
            "total_hours": "9.00",
            "timesheet_entries": [
                {
                    "hours": "9.00",
                    "project": {
                        "id": 8,
                        "client": {
                            "id": 3,
                            "created": "2019-04-02T13:49:21.437562Z",
                            "modified": "2019-04-02T13:49:21.437584Z",
                            "name": "Sp33d"
                        },
                        "roles": [
                            {
                                "name": "Development"
                            },
                            {
                                "name": "Design"
                            },
                            {
                                "name": "Testing"
                            }
                        ],
                        "created": "2019-04-02T14:35:13.410254Z",
                        "modified": "2019-04-02T14:35:13.410277Z",
                        "name": "Speed-HR",
                        "type": "internal"
                    },
                    "project_role": {
                        "name": "Development"
                    }
                }
            ]
        },

这是我必须执行update_or_create的代码

class TimesheetEntrySerializer(ModelSerializer):
    project = serializers.PrimaryKeyRelatedField(
        queryset=Project.objects.all(), label=_("Project"),
        style={"value_accessor": "id"}
    )
    project_role = serializers.PrimaryKeyRelatedField(
        queryset=ProjectRole.objects.all(), label=_("Project role"),
        style={"value_accessor": "id"}
    )

    class Meta:
        model = TimesheetEntry
        fields = ['hours', 'project', 'project_role']

    def to_representation(self, instance):
        result = super().to_representation(instance)
        result['project'] = ProjectSerializer(instance=instance.project).data
        result['project_role'] = ProjectRoleSerializer(instance=instance.project_role).data
        return result


class TimesheetSerializer(ModelSerializer):
    user = serializers.PrimaryKeyRelatedField(
        queryset=User.objects.all(), label=_("User"),
        style={"value_accessor": "pk"}
    )

    timesheet_entries = TimesheetEntrySerializer(many=True)

    class Meta:
        model = Timesheet
        fields = ['id', 'date', 'user', 'total_hours', 'timesheet_entries']

    def to_representation(self, instance):
        result = super().to_representation(instance)
        result['user'] = UserDetailsSerializer(instance=instance.user).data

        return result

    def create(self, validated_data):
        entries = validated_data.pop('timesheet_entries')

        timesheet = Timesheet.objects.create(**validated_data)

        for entry in entries:
            TimesheetEntry.objects.create(timesheet=timesheet, **entry)

        timesheet.recalculate()
        timesheet.save()

        return timesheet

    def update(self, instance, validated_data):
        te = validated_data.pop('timesheet_entries', [])
        timesheet_entries = super().update(instance, validated_data)

        for te in timesheet_entries:
            u, create = TimesheetEntry.objects.update_or_create(
                timesheet_entries=instance
            )

0 个答案:

没有答案