Date,TimeField, other field serialization formatting

86 views
Skip to first unread message

José Moreira

unread,
Apr 18, 2011, 1:30:10 AM4/18/11
to django-piston
Hello,

i'm wondering how the community is dealing with Date/TimeField
serialization output formatting (strftime).

Although formatting should happen on the client-side, i have at least
a use case, timedelta's, rough. ex "1 day, 1:01" to "1 day, one hour,
1 minute".
The standard seems to be creating a display property on the models,
'duration_display' and using it on the resource fields, but that also
renames the data field on the output.
So the other solution i know is to return a custom dict, but that
blows the lid a bit on ModelResource.

Any suggestions?

Best

José Moreira

unread,
Apr 18, 2011, 1:51:51 AM4/18/11
to django-piston
A 3rd solution would be subclassing Emmiter and formatting at will,
but the "problem" i see now is that, as seen on:

#https://bitbucket.org/jespern/django-piston/src/c4b2d21db51a/piston/
emitters.py#cl-98
def construct(self):
"""
Recursively serialize a lot of types, and
in cases where it doesn't recognize the type,
it will fall back to Django's `smart_unicode`.

Returns `dict`.
"""
def _any(thing, fields=None):
"""
Dispatch, all types are routed through here.
"""
ret = None

if isinstance(thing, QuerySet):



the _any method is a inner function of the construct method, so afaik
to subclass i and do custom formatting, would have to subclass the
whole method.

Am i missing anything ?

Dan Fairs

unread,
Apr 18, 2011, 6:56:51 AM4/18/11
to django...@googlegroups.com
> the _any method is a inner function of the construct method, so afaik
> to subclass i and do custom formatting, would have to subclass the
> whole method.
>

For what it's worth, this is what we ended up doing on a recent project.
We moved all those inner functions to being proper functions as well, to
make future customisation easier.

Cheers,
Dan

--
Dan Fairs | dan....@gmail.com | www.fezconsulting.com

Jeremy Sandell

unread,
Apr 18, 2011, 1:02:31 PM4/18/11
to django...@googlegroups.com
That's quite similar to how I did it. I patched _any to call self.default(thing) instead of smart_unicode, then added a small method named "default", which just defaults to running smart_unicode(thing, strings_only=True). That way it's simple to inherit and extend the behavior for types that piston isn't aware of. (I didn't need to override anything that it already knew about, though.)

Best regards,
Jeremy Sandell



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


Chris Wesseling

unread,
Apr 18, 2011, 3:40:55 AM4/18/11
to django...@googlegroups.com
On 2011-04-17T22:51:51-0700, José Moreira wrote:
> > i'm wondering how the community is dealing with Date/TimeField
> > serialization output formatting (strftime).
[... put ..._display() in resource fields, return a custom dict ...]

> A 3rd solution would be subclassing Emmiter and formatting at will,
> but the "problem" i see now is that, as seen on:
>
> #https://bitbucket.org/jespern/django-piston/src/c4b2d21db51a/piston/
> emitters.py#cl-98
> def construct(self):
> """
> Recursively serialize a lot of types, and
> in cases where it doesn't recognize the type,
> it will fall back to Django's `smart_unicode`.
>
> Returns `dict`.
> """
> def _any(thing, fields=None):
> """
> Dispatch, all types are routed through here.
> """
> ret = None
>
> if isinstance(thing, QuerySet):
>
>
>
> the _any method is a inner function of the construct method, so afaik
> to subclass i and do custom formatting, would have to subclass the
> whole method.
>
> Am i missing anything ?

You could implement __emittable__ on the Model you're serializing, and
use the default emitters.

--
Chris
--
A: No.
Q: Should I include quotations after my reply?

Reply all
Reply to author
Forward
0 new messages