You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lot of the times, I want to serialize an object, but not send all of its properties. This becomes cumbersome with Django models.
There should be some way to tell the serializer what fields to skip or include, in the same way as with the built-in Django JSON serializer.
I've made a custom hack for this - I am sure this could be solved in some more elegant way.
Diff on util/**init**.py:
< # special FileField handling (they can't be json serialized)
< if isinstance(f, ImageField) or isinstance(f, FileField):
< ret[f.attname] = unicode(getattr(data, f.attname))
< else:
## < ret[f.attname] = _any(getattr(data, f.attname))
> if not (hasattr(data, 'CustomMeta') and hasattr(data.CustomMeta, 'serialize_fields') and not f.attname in data.CustomMeta.serialize_fields):
> # special FileField handling (they can't be json serialized)
> if isinstance(f, ImageField) or isinstance(f, FileField):
> ret[f.attname] = unicode(getattr(data, f.attname))
> else:
> ret[f.attname] = _any(getattr(data, f.attname))
158c159
## < add_ons = [k for k in dir(data) if k not in fields and k not in ('delete', '_state',)]
> add_ons = [k for k in dir(data) if k not in fields and k not in ('delete', '_state',) and not (hasattr(data, 'CustomMeta') and hasattr(data.CustomMeta, 'serialize_fields') and not f.attname in data.CustomMeta.serialize_fields)]
How it's used in a model:
class MyModel(Model):
id = CharField(max_length=24, primary_key=True)
name = CharField(max_length=20)
secret = CharField(max_length=20)
class CustomMeta:
serialize_fields = ('id', 'name')
Original link: http://code.google.com/p/dojango/issues/detail?id=72
The text was updated successfully, but these errors were encountered:
Original link: http://code.google.com/p/dojango/issues/detail?id=72
The text was updated successfully, but these errors were encountered: