Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#24320 closed Bug (fixed)

Unable to serialize UUIDField when running dumpdata with JSON format

Reported by: James Beith Owned by: Andriy Sokolovskiy
Component: Core (Serialization) Version: 1.8alpha1
Severity: Release blocker Keywords:
Cc: me@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by James Beith)

A model using a UUIDField as its primary key. Running dumpdata management command with JSON output format (the default option) resulted in the following error

CommandError: Unable to serialize database: UUID('435697b4-954a-4b9a-a83a-2b53016b0d43') is not JSON serializable

However running again with --format=xml did not result in an error and dumpdata was successful.

Attachments (3)

24320-test.diff (1.5 KB ) - added by Tim Graham 10 years ago.
uuid-dumpdata.zip (109.2 KB ) - added by James Beith 10 years ago.
Example project
24320-test.2.diff (2.8 KB ) - added by Tim Graham 10 years ago.

Download all attachments as: .zip

Change History (17)

comment:1 by James Beith, 10 years ago

Description: modified (diff)
Summary: Unable to serialize UUID field when running dumpdata with JSON formatUnable to serialize UUIDField when running dumpdata with JSON format

comment:2 by Claude Paroz, 10 years ago

Component: UtilitiesCore (Serialization)
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 10 years ago

I created a custom user with UUID pk:

import uuid

from django.contrib.auth.models import AbstractUser
from django.db import models


class UUIDUser(AbstractUser):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)

And then tried:

python manage.py dumpdata uuiduser.UUIDUser
[{"fields": {"username": "tim", "first_name": "", "last_name": "", "is_active": true,
"is_superuser": true, "is_staff": true, "last_login": "2015-02-11T19:01:02.747Z",
"groups": [], "user_permissions": [], "password": "...", "email": "a@g.com",
"date_joined": "2015-02-11T18:55:33.572Z"}, "model": "uuiduser.uuiduser",
"pk": "dd100fcd-a894-4435-ad30-bdcd4671ac44"}]

Tested on SQLite and PostgreSQL. Could you provide more details? I'm also attached a test for Django's test suite that I tried writing to reproduce the issue.

by Tim Graham, 10 years ago

Attachment: 24320-test.diff added

comment:4 by Tim Graham <timograham@…>, 10 years ago

In d7509c52466aeffb13ccd8a86475e74fdb0e6292:

Added a test for UUIDField serialization; refs #24320.

comment:5 by Tim Graham <timograham@…>, 10 years ago

In 8d1816db87e9e700d9bd5336ac25686fdf30c789:

[1.8.x] Added a test for UUIDField serialization; refs #24320.

Backport of d7509c52466aeffb13ccd8a86475e74fdb0e6292 from master

comment:6 by Tim Graham, 10 years ago

Resolution: needsinfo
Status: newclosed

Please reopen with more details if this is still an issue.

comment:7 by James Beith, 10 years ago

Resolution: needsinfo
Status: closednew

I have created an example project which reproduces the issue.

by James Beith, 10 years ago

Attachment: uuid-dumpdata.zip added

Example project

comment:8 by Tim Graham, 10 years ago

Thanks. Problem seems to be having a FK to an object with a UUID pk. Attaching a failing test for Django's test suite.

by Tim Graham, 10 years ago

Attachment: 24320-test.2.diff added

comment:9 by Andriy Sokolovskiy, 10 years ago

Owner: changed from nobody to Andriy Sokolovskiy
Status: newassigned

I will try to fix this

comment:10 by Andriy Sokolovskiy, 10 years ago

Cc: me@… added
Has patch: set

comment:11 by Tim Graham, 10 years ago

There are at least two open tickets related to this that may be duplicates or warrant concurrent investigation: #9422 and #17587. By the way, it's easier if you open your PR against master and let the committer backport from there.

comment:12 by Andriy Sokolovskiy, 10 years ago

comment:13 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

In 5c995dcfc251b55284e1ef16545acd2acad6be04:

Fixed #24320 - Used field.value_to_string() in serialization of foreign key.

This fixes serialization of a ForeignKey to a UUIDField as the
test indicates.

comment:14 by Tim Graham <timograham@…>, 10 years ago

In 136edac8974ff0b570ce0a3e66e7aadf542225f0:

[1.8.x] Fixed #24320 - Used field.value_to_string() in serialization of foreign key.

This fixes serialization of a ForeignKey to a UUIDField as the
test indicates.

Backport of 5c995dcfc251b55284e1ef16545acd2acad6be04 from master

Note: See TracTickets for help on using tickets.
Back to Top