Opened 13 years ago

Closed 10 years ago

#17587 closed Bug (duplicate)

serializing foreignkeys assumes value is serializable

Reported by: Alex Burgel Owned by: nobody
Component: Core (Serialization) Version: 1.3
Severity: Normal Keywords:
Cc: Alex Burgel Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

in django.core.serializers.python, handle_fk_field and handle_m2m_field assume that the foreignkey value requires no conversion, i.e. value_to_string is not called. handle_fk_field sometimes does not even convert the foreignkey value to a string (line 58).

if you're using custom fields whose values are not serializable, then you're given no opportunity for conversion, and in the case of json, if that value isn't a native json type, then you get an exception.

the way to work around this is to write your own serializer or to use natural keys, but given that value_to_string exists, it should be used.

Attachments (1)

fk_serialize.patch (5.4 KB ) - added by Alex Burgel 13 years ago.

Download all attachments as: .zip

Change History (8)

comment:1 by Claude Paroz, 13 years ago

A test case would be nice, or at least an example model wich would surely fail to serialize properly.

comment:2 by Alex Burgel, 13 years ago

Cc: Alex Burgel added

added a failing test case. i'm working on a fix as well.

comment:3 by Alex Burgel, 13 years ago

added a patch with a test case and a fix for both python and xml serializers.

i didn't tackle the m2m case, but from the code it looks like you can only use a pk or a natural key, so maybe its not an issue.

comment:4 by Alex Burgel, 13 years ago

Has patch: set

btw, the patch is off the git mirror of the 1.3.X branch. let me know if i should create it off something different.

comment:5 by Claude Paroz, 13 years ago

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Yes, please provide the patch against master.

by Alex Burgel, 13 years ago

Attachment: fk_serialize.patch added

comment:6 by Alex Burgel, 13 years ago

ok, patched against master.

comment:7 by Claude Paroz, 10 years ago

Resolution: duplicate
Status: newclosed

Apparently, #24320 fixed this issue in 5c995dcfc251b55284e1ef16545acd2acad6be04.

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