django-rest-framework-bulk icon indicating copy to clipboard operation
django-rest-framework-bulk copied to clipboard

[enhancement/bug fix]: UUID primary key support on update

Open marcgibbons opened this issue 8 years ago • 3 comments

Using models with a UUIDField as a primary key fails on bulk updates. This happens because, the validated_data dictionary value for the id will be a string, while the internal data type is a UUID. Therefore, this line fails to pull the values from the dictionary:

obj_validated_data = all_validated_data_by_id.get(obj_id)

serializers.py line 64 E.g

# if all_validated_data_by_id = {'200841c5-09cd-4a92-b60a-96f28750ecf9': {...}}
all_validated_data_by_id.get(UUID('200841c5-09cd-4a92-b60a-96f28750ecf9')) == None

This causes a NoneType error to be raised if obj is None here: updated_objects.append(self.child.update(obj, obj_validated_data))

This PR casts the object_id to a string if it is of type UUID. Test included.

marcgibbons avatar Dec 16 '15 17:12 marcgibbons

The solution makes sense. Tests seem to fail on Django<1.8 since UUIDField was not available yet. Probably some magic will have to be added to make it work on earlier versions.

miki725 avatar Dec 16 '15 17:12 miki725

@miki725 Totally! Updating now

marcgibbons avatar Dec 16 '15 17:12 marcgibbons

@marcgibbons thanks for the PR. just had some minor comments. let me know if you would like to take care of them or should I. this is pretty much ready to rock-and-roll :guitar:

miki725 avatar Dec 17 '15 18:12 miki725