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?
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.
One trick I regularly use is to define fields as a list instead of the tuple and then append to it in read(). I don't know if there are issues with doing it but so far it's worked great:
class PlayerHandler(BaseHandler): allowed_methods = ('GET',) model = Player fields = [ 'id', 'name', 'level', 'hp', 'max_hp', ]
def read(self, request, id): player = Player.objects.get(pk=id)
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.
> -- > You received this message because you are subscribed to the Google Groups > "django-piston" group. > To post to this group, send email to django-piston@googlegroups.com. > To unsubscribe from this group, send email to > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-piston?hl=en.
> One trick I regularly use is to define fields as a list instead of the tuple > and then append to it in read(). I don't know if there are issues with doing > it but so far it's worked great:
> class PlayerHandler(BaseHandler): > allowed_methods = ('GET',) > model = Player > fields = [ > 'id', > 'name', > 'level', > 'hp', > 'max_hp', > ]
> def read(self, request, id): > player = Player.objects.get(pk=id)
> 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.
> > -- > > You received this message because you are subscribed to the Google Groups > > "django-piston" group. > > To post to this group, send email to django-piston@googlegroups.com. > > To unsubscribe from this group, send email to > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/django-piston?hl=en.
I do but it's a scenario where each threads dies out after a couple seconds and is then replaced by a new one so it's possible that masks the side effects... which part do you see affecting threading? is it using a list instead of a tuple that's the problem?
On Mon, Feb 8, 2010 at 9:37 AM, jespern <jno...@gmail.com> wrote: > On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: > > One trick I regularly use is to define fields as a list instead of the > tuple > > and then append to it in read(). I don't know if there are issues with > doing > > it but so far it's worked great:
> 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.
> > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "django-piston" group. > > > To post to this group, send email to django-piston@googlegroups.com. > > > To unsubscribe from this group, send email to > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > <django-piston%2Bunsubscribe@goog legroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/django-piston?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "django-piston" group. > To post to this group, send email to django-piston@googlegroups.com. > To unsubscribe from this group, send email to > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-piston?hl=en.
No, it's the fact that you're modifying 'self.fields'. The handler is only instantiated once (per interpreter), so the next request being passed through this handler will likely suffer the side effects.
Jesper
On Feb 8, 4:54 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote:
> I do but it's a scenario where each threads dies out after a couple seconds > and is then replaced by a new one so it's possible that masks the side > effects... which part do you see affecting threading? is it using a list > instead of a tuple that's the problem?
> On Mon, Feb 8, 2010 at 9:37 AM, jespern <jno...@gmail.com> wrote: > > On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: > > > One trick I regularly use is to define fields as a list instead of the > > tuple > > > and then append to it in read(). I don't know if there are issues with > > doing > > > it but so far it's worked great:
> > 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.
> > > > -- > > > > You received this message because you are subscribed to the Google > > Groups > > > > "django-piston" group. > > > > To post to this group, send email to django-piston@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > > <django-piston%2Bunsubscribe@goog legroups.com> > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/django-piston?hl=en.
> > -- > > You received this message because you are subscribed to the Google Groups > > "django-piston" group. > > To post to this group, send email to django-piston@googlegroups.com. > > To unsubscribe from this group, send email to > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/django-piston?hl=en.
On Mon, Feb 8, 2010 at 9:57 AM, jespern <jno...@gmail.com> wrote: > No, it's the fact that you're modifying 'self.fields'. The handler is > only instantiated once (per interpreter), so the next request being > passed through this handler will likely suffer the side effects.
> Jesper
> On Feb 8, 4:54 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: > > I do but it's a scenario where each threads dies out after a couple > seconds > > and is then replaced by a new one so it's possible that masks the side > > effects... which part do you see affecting threading? is it using a list > > instead of a tuple that's the problem?
> > On Mon, Feb 8, 2010 at 9:37 AM, jespern <jno...@gmail.com> wrote: > > > On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: > > > > One trick I regularly use is to define fields as a list instead of > the > > > tuple > > > > and then append to it in read(). I don't know if there are issues > with > > > doing > > > > it but so far it's worked great:
> > > 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://
> > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "django-piston" group. > > > > > To post to this group, send email to > django-piston@googlegroups.com. > > > > > To unsubscribe from this group, send email to > > > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > <django-piston%2Bunsubscribe@goog legroups.com> > > > <django-piston%2Bunsubscribe@goog legroups.com> > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/django-piston?hl=en.
> > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "django-piston" group. > > > To post to this group, send email to django-piston@googlegroups.com. > > > To unsubscribe from this group, send email to > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > <django-piston%2Bunsubscribe@goog legroups.com> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/django-piston?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "django-piston" group. > To post to this group, send email to django-piston@googlegroups.com. > To unsubscribe from this group, send email to > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-piston?hl=en.
Sorry about the confusion, I realize now I had misunderstood the original question, as well the proposed solution. I've played with list_fields and I understand its use case and how it answers the question. That is definitely a nice feature.
I still wonder if there is a good way to do what I am trying to do though? adding a set of fields to a resource if it matches a certain criteria. For example, one resource could return a different set of fields when viewed as a regular user vs a staff user. Or, as in my earlier example, a resource showing more details to its owner than to any third party viewer?
I understand that changing the fields after the handler is instanciated is problematic, but then I don't see a clear way to do that would be? any thoughts would be appreciated, and very sorry about all the added noise.
On Mon, Feb 8, 2010 at 9:59 AM, Thibaud Morel l'Horset <tee...@gmail.com>wrote:
> OK thanks for the heads-up, I'll try using list_fields instead
> On Mon, Feb 8, 2010 at 9:57 AM, jespern <jno...@gmail.com> wrote:
>> No, it's the fact that you're modifying 'self.fields'. The handler is >> only instantiated once (per interpreter), so the next request being >> passed through this handler will likely suffer the side effects.
>> Jesper
>> On Feb 8, 4:54 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: >> > I do but it's a scenario where each threads dies out after a couple >> seconds >> > and is then replaced by a new one so it's possible that masks the side >> > effects... which part do you see affecting threading? is it using a list >> > instead of a tuple that's the problem?
>> > On Mon, Feb 8, 2010 at 9:37 AM, jespern <jno...@gmail.com> wrote: >> > > On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote: >> > > > One trick I regularly use is to define fields as a list instead of >> the >> > > tuple >> > > > and then append to it in read(). I don't know if there are issues >> with >> > > doing >> > > > it but so far it's worked great:
>> > > 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://
>> > > > > -- >> > > > > You received this message because you are subscribed to the Google >> > > Groups >> > > > > "django-piston" group. >> > > > > To post to this group, send email to >> django-piston@googlegroups.com. >> > > > > To unsubscribe from this group, send email to >> > > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> >> <django-piston%2Bunsubscribe@goog legroups.com> >> > > <django-piston%2Bunsubscribe@goog legroups.com> >> > > > > . >> > > > > For more options, visit this group at >> > > > >http://groups.google.com/group/django-piston?hl=en.
>> > > -- >> > > You received this message because you are subscribed to the Google >> Groups >> > > "django-piston" group. >> > > To post to this group, send email to django-piston@googlegroups.com. >> > > To unsubscribe from this group, send email to >> > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> >> <django-piston%2Bunsubscribe@goog legroups.com> >> > > . >> > > For more options, visit this group at >> > >http://groups.google.com/group/django-piston?hl=en.
>> -- >> You received this message because you are subscribed to the Google Groups >> "django-piston" group. >> To post to this group, send email to django-piston@googlegroups.com. >> To unsubscribe from this group, send email to >> django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com> >> . >> For more options, visit this group at >> http://groups.google.com/group/django-piston?hl=en.
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)
> 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)