Opened 5 months ago
Closed 5 months ago
#35588 closed Uncategorized (invalid)
Migrating primary key to UUIDField causes serializer validation issues
Reported by: | DaanSterk | Owned by: | |
---|---|---|---|
Component: | Uncategorized | Version: | |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
For application-specific performance considerations, we recently decided to migrate our primary keys from BigAutoField to UUIDField.
class BaseModel(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
All models extend BaseModel. For example:
class Message(BaseModel): messenger = models.ForeignKey('core.PersonBusinessUmbrella', on_delete=models.CASCADE)
There is a lot of (writable) nesting involved in our serializers. For example:
class CustomModelSerializer(ModelSerializer): created_at = serializers.DateTimeField(required=False, allow_null=True) class MessageSerializer(CustomModelSerializer): messenger = PersonBusinessUmbrellaSerializer(required=False) class PersonBusinessUmbrellaSerializer(CustomModelSerializer): business_umbrella = BusinessUmbrellaSerializer() class BusinessUmbrellaSerializer(CustomModelSerializer): ...
All data retrieval continues to work normally. However, when sending POST/PUT requests with that same data structure to the corresponding ModelViewSet, issues arise.
POST-ing a new Message, including nested models
{ "uuid": "36d562d9-5127-4591-a532-3900c639156a", "relatedFiles": [], "order": "00e204d9-ef4e-482c-bd5d-8a4129450728", "messenger": { "id": "b3756ec6-d793-4a4e-88f7-5826e38f83d3", "uuid": "9141eb64-2067-4e22-adb7-4d51d7723565", "createdAt": "2024-07-09T09:38:22.906", "updatedAt": "2024-07-09T09:38:22.906", "person": { "id": "445da4a0-9ab6-4bad-9d83-40e9215d269f", "uuid": "d74e9c15-9e0d-429e-b9e8-d63c5d68edc7", "createdAt": "2024-07-09T09:38:22.229", "updatedAt": "2024-07-09T09:38:22.229", "businessUmbrellas": [ { "id": "b0cfda01-80df-456c-9603-1b9d68b67270", "uuid": "51007185-c51b-4dd9-b5ff-d615706abc25", "createdAt": "2024-07-09T09:38:22.129", "updatedAt": "2024-07-09T09:38:22.129", "name": "Bouwman", "comment": null } ], "firstName": "Adam", "lastName": "Willems", "phoneNumber": "0611666677" }, "businessUmbrella": { "id": "b0cfda01-80df-456c-9603-1b9d68b67270", "uuid": "51007185-c51b-4dd9-b5ff-d615706abc25", "createdAt": "2024-07-09T09:38:22.129", "updatedAt": "2024-07-09T09:38:22.129", "name": "Bouwman", "comment": null }, "email": "adriana05@example.net", "phoneNumber": null, "roles": [ { "id": "a75be758-b0f1-45b3-8c8b-f77136d133b0", "uuid": "40d0e317-fdbf-4923-842f-8e665863ec26", "createdAt": "2024-07-09T09:38:25.928", "updatedAt": "2024-07-09T09:38:25.928", "personBusinessUmbrella": "b3756ec6-d793-4a4e-88f7-5826e38f83d3", "type": 2, "defaultMachine": { "id": "3fc4f1ce-9293-4d8c-a5f8-fa710024815e", "uuid": "7bc16327-07ac-4df2-8c40-4adaa206163d", "createdAt": "2024-07-09T09:38:22.198", "updatedAt": "2024-07-09T09:38:22.198", "name": "Heftruck-hebben" } } ] }, "body": "abc" }
Response
{ "messenger": { "nonFieldErrors": [ "‘OrderedDict({'created_at': datetime.datetime(2024, 7, 9, 9, 38, 22, 129000), 'uuid': UUID('51007185-c51b-4dd9-b5ff-d615706abc25'), 'name': 'Bouwman', 'comment': None})’ is geen geldige UUID." ] } }
Whereas with BigAutoField the serializer validation would understand that the nested OrderedDict represented an entity, it now insists on receiving an actual UUID value.
Change History (1)
comment:1 by , 5 months ago
Component: | Core (Serialization) → Uncategorized |
---|---|
Resolution: | → invalid |
Status: | new → closed |
Type: | Bug → Uncategorized |
Version: | 4.1 |
Hello DaanSterk, thank you for taking the time to create this ticket.
Based on your description, it appears that you are using Django Rest Framework for the serializers and view sets. This issue tracker specifically handles matters related to Django itself. Since your issue appears to reside either in your custom code or in Django Rest Framework (which is a separate and independent project), I will be closing this ticket as invalid in accordance following the ticket triaging process.
The best place to get answers to your issue is using any of the user support channels from DRF. If, upon further debugging, you determine that the issue is indeed a bug within Django, please feel free to reopen this ticket with specific details. Please ensure to include a minimal Django project or a failing test case that demonstrates the issue independently of any other dependencies.