#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": "[email protected]",
         "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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701909c6b4ccd-84b1b4a2-139f-4fdf-9fdc-619de284aaeb-000000%40eu-central-1.amazonses.com.

Reply via email to