This is the code I use (if your e-mail doesn't preview code well:
http://paste2.org/p/1921598):

    def serialize(self, localtz=None):
        """Converts all the properties of the object into a dict
        for use in json
        """
        props = {}

        def is_valid_key(key):
            if not key.startswith('_') and \
               not 'password' in key and \
               not 'salt' == key:
               return True

        _items = self.__class__.__dict__
        local_at = dict()
        if _items.has_key('timezone') and (getattr(self, 'timezone') or
localtz):
            timezone = localtz or getattr(self,'timezone')
            if _items.has_key('start_time') and getattr(self,'start_time'):
                local_at.update(self._localize_time('start_date',
'start_time', timezone.name))
            if _items.has_key('end_time') and getattr(self,'end_time'):
                local_at.update(self._localize_time('end_date', 'end_time',
timezone.name))

        for key, value in _items.items():
            if is_valid_key(key):
                obj = getattr(self, key)
                if hasattr(obj, 'serialize'):
                    props[key] = obj.serialize()
                elif not callable(obj):
                    if isinstance(obj, datetime) or isinstance(obj, date):
                        _date = local_at.get(key,None) or obj
                        props[key] = _date.strftime("%Y-%m-%d")
                    elif isinstance(obj, time):
                        _time = local_at.get(key,None) or obj
                        props[key] = _time.strftime("%I:%M %p")
                    else:
                        props[key] = getattr(self, key)

        props['id'] = str(self.pk)

        return props



On Thu, Mar 1, 2012 at 9:30 AM, Cosmia Luna <[email protected]> wrote:

> PS: I meant "you can try to look into the Base.__table__.columns object
>
> Regards,
> Cosmia
>
>
> On Thursday, March 1, 2012 11:24:54 PM UTC+8, Cosmia Luna wrote:
>>
>> May be you can try to look into the just add such a line to the base, it
>> can help you get the list of defined properties of the model,
>>
>> class Base(object):
>>     def __init__(self, *args, **kwargs):
>>         # do your business here
>>         self._to_serialize=self.__**table__.columns.keys()
>>         # do some other thing or add other stuff you want to serialize
>>
>> But it can only return those explicitly defined columns, properties won't
>> be returned, you can add them later.
>>
>> Regards,
>> Cosmia
>>
>> On Thursday, March 1, 2012 11:04:50 PM UTC+8, Alexis Metaireau wrote:
>>>
>>> What I'm doing is to add a property on the models I want to serialize,
>>> and to use a custom json serializer for this purpose.
>>>
>>> Have a look at
>>> https://github.com/spiral-**project/ihatemoney/blob/**
>>> master/budget/models.py#L12<https://github.com/spiral-project/ihatemoney/blob/master/budget/models.py#L12>
>>> and
>>> https://github.com/ametaireau/**flask-rest/blob/master/flask_**
>>> rest.py#L144<https://github.com/ametaireau/flask-rest/blob/master/flask_rest.py#L144>
>>>
>>> However, this isn't automatic, and it is probably possible to automate
>>> this without having to explicitly specify what you want to serialize
>>> (like Julien did)
>>>
>>>  --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/pylons-discuss/-/TOFfc3SAPXEJ.
>
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to