django - How to return only one field in tastypie? -


suppose have resource below..

class postresource(modelresource):      children = fields.tomanyfield('myapp.api.resources.postresource',                  attribute='comments', full=true, null=true) 

basically, want return children field , flatten it.

it like
[ {child-1-data}, {child-2-data} ]
rather { children: [ {child-1-data}, {child2-data} ] }

how can that?

additionaly, if want different representation of same model class, should create new resource class bellow?

class postnormalresource(modelresource):       class meta:           queryset= models.post.objects.all()           fields = ['text', 'author'] 

not answer looking discoveries made while digging.

normally modify bundle data in dehydrate. see tastypie cookbook.

def dehydrate(self, bundle):     bundle.data['custom field'] = "this additional text on resource"     return bundle 

this suggest manipulate postresource's bundle data along lines of:

def dehydrate(self, bundle):     # replace data list of children     bundle.data = bundle.data['children']     return bundle 

however error, attributeerror: 'list' object has no attribute 'items', tastypie serializer looking serialize dictionary not list.

# "site-packages/tastypie/serializers.py", line 239 return dict((key, self.to_simple(val, options)) (key, val) in data.data.items())  # .items() being dicts 

so suggests need @ different serializers. (or refer post['children'] when processing json :-)

hope helps in right direction


and secondly yes, if want different representation of same model use second modelresource. can subclass try , avoid duplication.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -