コード例 #1
0
    def test_might_have_changes_returns_true(self):
        justin = Person(first_name='Justin', last_name='Trudeau')
        justin.save()

        serializer = PersonSerializer(justin, data={
            'first_name': 'Justin',
            'last_name': 'Trudeau2',
            'partner': ''
        })

        might_have_changes = serializers.might_have_changes(serializer)
        self.assertTrue(might_have_changes)
コード例 #2
0
    def process_row(self, row, data, context):
        if data.processed:
            return

        serializer = self.serializer(instance=data.instance,
                                     data=row.data.copy(),
                                     context=context,
                                     partial=data.instance is not None)

        if not serializers.might_have_changes(serializer):
            row.status = RowStatus.unchanged
            data.processed = True
            return

        is_valid = serializer.is_valid()
        has_changes = serializers.has_changes(serializer)

        cannot_update = (
            data.instance
            and (has_changes or not is_valid)
            and not self.can_update_object(data.instance)
        )

        if cannot_update:
            row.set_error(self.error_messages['cannot_update'])
            data.processed = True
            return

        if not is_valid:
            row.set_errors(serializer.errors)
            data.processed = True
            return

        if not has_changes:
            row.status = RowStatus.unchanged
            data.processed = True
            return

        if serializer.instance:
            row.status = RowStatus.update
            row.diff = serializers.get_diff_data(serializer)
            serializer.save()
            data.processed = True

        else:
            row.status = RowStatus.new
            row.diff = serializers.get_diff_data(serializer)
            data.instance = serializer.save()
            data.processed = True
            self.cache_instance(context, data.instance)