Opened 14 years ago
Closed 11 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)
Change History (8)
comment:1 by , 14 years ago
comment:3 by , 14 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 , 14 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 , 14 years ago
| Patch needs improvement: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
Yes, please provide the patch against master.
by , 14 years ago
| Attachment: | fk_serialize.patch added |
|---|
comment:7 by , 11 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Apparently, #24320 fixed this issue in 5c995dcfc251b55284e1ef16545acd2acad6be04.
A test case would be nice, or at least an example model wich would surely fail to serialize properly.