fastuuid icon indicating copy to clipboard operation
fastuuid copied to clipboard

ValidationError when using fastuuid with django's UUID field

Open LegNeato opened this issue 4 years ago • 5 comments

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.']

LegNeato avatar Feb 17 '21 09:02 LegNeato

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.

thedrow avatar Feb 18 '21 11:02 thedrow

Great, I'll try to get time to put up a PR in the next week or so, thanks for looking so quickly!

LegNeato avatar Feb 18 '21 17:02 LegNeato

This similarly happens with SQLAlchemys Uuid column, which is stopping us from adopting this package

ollz272 avatar Apr 17 '23 16:04 ollz272

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

LegNeato avatar Apr 17 '23 19:04 LegNeato