Opened 11 years ago

Closed 11 years ago

#5574 closed (fixed)

FileField not properly serializing to XML

Reported by: Chris Henderson <chris@…> Owned by: prairiedogg
Component: Core (Serialization) Version: master
Severity: Keywords: FileField, xml, serialization
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When executing python dumpdata --format xml, FileField fields get serialized using the get_FIELD_url() method, which is causing (for me at least) this error:

Problem installing fixture 'myfixture.xml': [Errno 2] No such file or directory: u'/site_media/avatars/system/001.gif

The small attached patch fixes this problem by not treating FileField fields differently than other fields.

Attachments (1)

base_serializer.diff (632 bytes) - added by Chris Henderson <chris@…> 11 years ago.

Download all attachments as: .zip

Change History (10)

Changed 11 years ago by Chris Henderson <chris@…>

Attachment: base_serializer.diff added

comment:1 Changed 11 years ago by Russell Keith-Magee

Resolution: worksforme
Status: newclosed

I can't reproduce the problem you describe. Furthermore, the serializers regressiontest includes cases that serialize FileFields with no actual file present.

If you can provide a sample model, fixture, and test procedure, feel free to reopen this ticket.

comment:2 Changed 11 years ago by anonymous

Resolution: worksforme
Status: closedreopened

The issue isn't on serializing, but on trying to reload the info.

The serializer code calls get_fieldname_url instead of just outputting the database value:

        elif isinstance(field, models.FileField):
            value = getattr(obj, "get_%s_url" %, lambda: None)()

So when you read it back in, it's got the wrong value - in his example, while the field originally held "avatars/system/001.gif", the serializers output "/site_media/avatars/system/001.gif"

comment:3 Changed 11 years ago by Collin Grady

Er, the issue seems to only show up in the XML serializer, now that I look (that last comment was me btw)

comment:4 Changed 11 years ago by Chris Henderson <chris@…>

The "no such file or directory" error I was getting and was noted in the report was actually related to a method being called from a pre_save signal being sent to resize an image, but the underlying problem does seem to be that the value in the database is changed as cgrady noted above. Sorry for the confusing report, but I do think that there is an actual problem in the XML serializer (which is calling the base serializer for this behavior).

comment:5 Changed 11 years ago by Marty Alchin

Keywords: fs-rf added

comment:6 Changed 11 years ago by Marty Alchin

Keywords: fs-rf removed

After further research, I think this ticket is best dealt with as part of serialization, rather than in #5361.

comment:7 Changed 11 years ago by Jacob

Triage Stage: UnreviewedAccepted

comment:8 Changed 11 years ago by prairiedogg

Owner: changed from nobody to prairiedogg
Status: reopenednew
Triage Stage: AcceptedReady for checkin

Jacob Kaplan-Moss said that this was OK to check in even though it does not have a test. I confirmed that running this patch against the full django test suite did not produce errors, and also that it fixed the bug in my production case.

comment:9 Changed 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [7293]) Fixed #5574 -- Serialize a FileField using its filename from the database, not
external URL. The problem showed up when reloading the data. Patch from Chris

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