fastuuid
fastuuid copied to clipboard
ValidationError when using fastuuid with django's UUID field
Doing a simple
-from uuid import uuid4
+from fastuuid import uuid4
On a django field:
id = models.UUIDField(primary_key=True, default=uuid4)
Gives me errors like the following:
django.core.exceptions.ValidationError: ['“eb457f28-7cdc-4068-894b-1d0adfeb3e8d” is not a valid UUID.']
Hi. This was never tested before so thank you for trying!
We'll either need a custom field or fix the code upstream.
Django checks that the object you are providing is the regular uuid.UUID object from the standard library and if it isn't it attempts to cast it and fails.
See here.
All you need to do is to override to_python to check against fastuuid's UUID type.
Another workaround is to make isinstance(value, uuid.UUID) pass the test as they have exactly the same interface.
Great, I'll try to get time to put up a PR in the next week or so, thanks for looking so quickly!
This similarly happens with SQLAlchemys Uuid column, which is stopping us from adopting this package
I no longer need this, hence no PR...but the offending django code has moved to https://github.com/django/django/blob/594fcc2b7427f7baf2cf1a2d7cd2be61467df0c3/django/db/models/fields/init.py#L2689