[Django] #35588: Migrating primary key to UUIDField causes serializer validation issues

7 views
Skip to first unread message

Django

unread,
Jul 10, 2024, 7:33:21 AM7/10/24
to django-...@googlegroups.com
#35588: Migrating primary key to UUIDField causes serializer validation issues
-------------------------------------+-------------------------------------
Reporter: DaanSterk | Type: Bug
Status: new | Component: Core
| (Serialization)
Version: 4.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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": "adri...@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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35588>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 10, 2024, 8:18:54 AM7/10/24
to django-...@googlegroups.com
#35588: Migrating primary key to UUIDField causes serializer validation issues
-------------------------------+--------------------------------------
Reporter: DaanSterk | Owner: (none)
Type: Uncategorized | Status: closed
Component: Uncategorized | Version:
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* component: Core (Serialization) => Uncategorized
* resolution: => invalid
* status: new => closed
* type: Bug => Uncategorized
* version: 4.1 =>

Comment:

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
[https://docs.djangoproject.com/en/dev/internals/contributing/triaging-
tickets/#closing-tickets ticket triaging process].

The best place to get answers to your issue is using any of the user
support channels from [https://www.django-rest-
framework.org/community/contributing/#issues 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35588#comment:1>
Reply all
Reply to author
Forward
0 new messages