Created
October 23, 2012 15:20
-
-
Save rasca/3939389 to your computer and use it in GitHub Desktop.
ImageSpecFieldsJSONSerializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from StringIO import StringIO | |
from django.core.serializers.json import Serializer as JSONSerializer | |
class ImageSpecFieldsJSONSerializer(JSONSerializer): | |
""" | |
Adds ImageSpecFields to the serialization | |
""" | |
def serialize(self, queryset, **options): | |
""" | |
Copy paste from django base.Serializer + ImageSpecFields | |
""" | |
self.options = options | |
self.stream = options.pop("stream", StringIO()) | |
self.selected_fields = options.pop("fields", None) | |
self.use_natural_keys = options.pop("use_natural_keys", False) | |
self.start_serialization() | |
for obj in queryset: | |
self.start_object(obj) | |
# Use the concrete parent class' _meta instead of the object's _meta | |
# This is to avoid local_fields problems for proxy models. Refs #17717. | |
concrete_model = obj._meta.concrete_model | |
for field in concrete_model._meta.local_fields: | |
if field.serialize: | |
if field.rel is None: | |
if self.selected_fields is None or field.attname in self.selected_fields: | |
self.handle_field(obj, field) | |
else: | |
if self.selected_fields is None or field.attname[:-3] in self.selected_fields: | |
self.handle_fk_field(obj, field) | |
for field in concrete_model._meta.many_to_many: | |
if field.serialize: | |
if self.selected_fields is None or field.attname in self.selected_fields: | |
self.handle_m2m_field(obj, field) | |
# Modifications start here | |
if hasattr(obj, '_ik'): | |
for field in obj._ik.spec_files: | |
if self.selected_fields is None or field.attname in self.selected_fields: | |
self.handle_spec_files(obj, field) | |
# Modifications end here | |
self.end_object(obj) | |
self.end_serialization() | |
return self.getvalue() | |
def handle_spec_files(self, obj, field): | |
if field.source_file: | |
value = field.url | |
else: | |
value = None | |
self._current[field.attname] = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment