Opened 17 years ago
Closed 16 years ago
#10301 closed (duplicate)
Refactor Serializer to support iteratation
| Reported by: | Paul Egan | Owned by: | nobody | 
|---|---|---|---|
| Component: | Core (Serialization) | Version: | 1.0 | 
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Design decision needed | |
| Has patch: | yes | Needs documentation: | no | 
| Needs tests: | no | Patch needs improvement: | no | 
| Easy pickings: | no | UI/UX: | no | 
Description
The [source:django/trunk/django/core/serializers/base.py Serializer] interface forces the result to be fully materialised before use.  This prevents it's use for streaming responses.
Currently the closest you can come is something like the following, but the response doesn't return until the xml document is complete:
response = HttpResponse(content_type='text/xml') serializer = serializers.get_serializer("xml")() serializer.serialize(queryset.iterator(), stream=response) return response
The attached patch adds an iterator method to Serializer which yields the serialised data in chunks.  This allows the serialisation of large query sets to be streamed to a response without using much memory:
serializer = serializers.get_serializer("xml")() xml_iter = serializer.iterator(queryset.iterator(), chunk_size=100) return HttpResponse(xml_iter, content_type='text/xml')
Attachments (1)
Change History (3)
by , 17 years ago
| Attachment: | serialzer_iterator.diff added | 
|---|
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → Design decision needed | 
|---|
Setting to DDN because it is a design decision. That said, I think this is a solid improvement and would be a nice thing to have in 1.1
comment:2 by , 16 years ago
| Resolution: | → duplicate | 
|---|---|
| Status: | new → closed | 
Duplicate of #5423 (although this patch like it could be more promising).
Patch to add iterator support to Serializer