Thank you a lot for this snippet!

I've wondered about how to export objects to excel in a "more natural way"
and this seems to be a great approach, considering all the different
gotchas.

Thank you for taking the time to write this.. I've been postponing this
little bit of research for the longest time.


Cheers,
AT

On Tue, Aug 16, 2011 at 3:11 PM, Nate <shevi...@gmail.com> wrote:

> Thank you for the suggestion.
> I actually ended up extending Django's json serializer.  Here is the
> code in case you or someone else needs to do something like this.
>
> from django.core.serializers.json import Serializer as JsonSerializer
> from django.utils.encoding import is_protected_type
> class DisplayNameJsonSerializer(JsonSerializer):
>
>    def handle_field(self, obj, field):
>        value = field._get_val_from_obj(obj)
>
>        #If the object has a get_field_display() method, use it.
>        display_method = "get_%s_display" % field.name
>        if hasattr(obj, display_method):
>            self._current[field.name] = getattr(obj, display_method)()
>
>        # Protected types (i.e., primitives like None, numbers, dates,
>        # and Decimals) are passed through as is. All other values are
>        # converted to string first.
>        elif is_protected_type(value):
>            self._current[field.name] = value
>        else:
>            self._current[field.name] = field.value_to_string(obj)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to