We extract the list of fields last minute, and there is a somewhat
undocumented feature that should provide what you're after.
It's called 'list_fields', and will override 'fields', in case you
return a list or a QuerySet.
The feature is here: http://bitbucket.org/jespern/django-piston/src/tip/piston/resource.py#cl-206
Jesper
--
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.
I don't think that's anywhere *near* thread-safe. Have you run this in
a threaded environment?
Jesper
>
>
>
>
> On Mon, Feb 8, 2010 at 9:11 AM, jespern <jno...@gmail.com> wrote:
> > On Feb 8, 4:08 pm, Vermus <vermus.jab...@gmail.com> wrote:
> > > How to create a different set of fields for a model? For example, I
> > > want in a particular case, request a list with a reduced set of
> > > fields, and in choosing from a list of elements in the UI, to seek a
> > > more complete set of fields to display the object. At the moment, I
> > > understand, I can not create two Handlers for one model (http://
> > > bitbucket.org/jespern/django-piston/wiki/FAQ#why-does-piston-use-
> > > fields-from-previous-handlers). What suggestions?
>
> > We extract the list of fields last minute, and there is a somewhat
> > undocumented feature that should provide what you're after.
>
> > It's called 'list_fields', and will override 'fields', in case you
> > return a list or a QuerySet.
>
> > The feature is here:
> >http://bitbucket.org/jespern/django-piston/src/tip/piston/resource.py...
>
> > Jesper
>
> > --
> > 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<django-piston%2Bunsubscribe@goog legroups.com>
To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
Jesper
To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
Thank you very much! But my REST frontend takes the answer as:
data=Data.objects.get(pk=id)
response = {"response" :
{"status":0,
"data": data
}
}
Why 'list_fields' overrides fields in response only in a list or a
QuerySet? Simple 'list' works in this case perfectly.
Well, I looked at the code and little understood how it works.
I changed the code a bit:
resourse.py
206 fields = handler.fields
if hasattr(handler, 'no_list_fields'):
fields = handler.no_list_fields
emmiters.py
107 elif isinstance(thing, dict):
ret = _dict(thing, fields=fields)
293 def _dict(data, fields=()):
"""
Dictionaries.
"""
return dict([ (k, _any(v, fields)) for k, v in
data.iteritems() ])
class ModelHandler(BaseHandler):
allowed_methods = ('GET',)
model = Model
no_list_fields = ('id', 'title', 'title_original', 'year', 'date',
'length', ('country', ('name',)), )
fields = ('id', 'title', 'title_original',)
def read(self, request):
if 1:
data = list(Model.objects.values_list(*self.fields))
return {"response" :
{"status":0,
"data": data
}
}
else:
data = Model.objects.get(pk=id)
return {"response" :
{"status":0,
"data": data
}
}
Now if pass list() to data, then the response returns the set of
'fields'*, in other cases (also, in future handlers) -
'no_list_fields'.
Criticize code, please.
*elif isinstance(thing, (tuple, list)):
ret = _list(thing)
It's better if you fork piston on Bitbucket, commit your changes, and
send us a link here.
Jesper