Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How to create a different set of fields for a model?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Vermus  
View profile  
 More options Feb 8 2010, 9:08 am
From: Vermus <vermus.jab...@gmail.com>
Date: Mon, 8 Feb 2010 06:08:05 -0800 (PST)
Local: Mon, Feb 8 2010 9:08 am
Subject: How to create a different set of fields for a model?
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?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jespern  
View profile  
 More options Feb 8 2010, 9:11 am
From: jespern <jno...@gmail.com>
Date: Mon, 8 Feb 2010 06:11:03 -0800 (PST)
Local: Mon, Feb 8 2010 9:11 am
Subject: Re: How to create a different set of fields for a model?
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thibaud Morel l'Horset  
View profile  
 More options Feb 8 2010, 9:18 am
From: "Thibaud Morel l'Horset" <tee...@gmail.com>
Date: Mon, 8 Feb 2010 09:18:33 -0500
Local: Mon, Feb 8 2010 9:18 am
Subject: Re: How to create a different set of fields for a model?

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)

        if player.user == request.user:
            self.fields += [
                'builder_mode',
                'items',
                'mp',
                'max_mp',
                'experience',
                'next_level',
                'equipment',
                'inventory',
            ]

        return player

- Teebes

--
Thibaud Morel l'Horset
http://teebes.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jespern  
View profile  
 More options Feb 8 2010, 9:37 am
From: jespern <jno...@gmail.com>
Date: Mon, 8 Feb 2010 06:37:55 -0800 (PST)
Local: Mon, Feb 8 2010 9:37 am
Subject: Re: How to create a different set of fields for a model?
On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <tee...@gmail.com> wrote:

I don't think that's anywhere *near* thread-safe. Have you run this in
a threaded environment?

Jesper


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thibaud Morel l'Horset  
View profile  
 More options Feb 8 2010, 9:54 am
From: "Thibaud Morel l'Horset" <tee...@gmail.com>
Date: Mon, 8 Feb 2010 09:54:53 -0500
Local: Mon, Feb 8 2010 9:54 am
Subject: Re: How to create a different set of fields for a model?

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?

--
Thibaud Morel l'Horset
http://teebes.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jespern  
View profile  
 More options Feb 8 2010, 9:57 am
From: jespern <jno...@gmail.com>
Date: Mon, 8 Feb 2010 06:57:12 -0800 (PST)
Local: Mon, Feb 8 2010 9:57 am
Subject: Re: How to create a different set of fields for a model?
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thibaud Morel l'Horset  
View profile  
 More options Feb 8 2010, 9:59 am
From: "Thibaud Morel l'Horset" <tee...@gmail.com>
Date: Mon, 8 Feb 2010 09:59:31 -0500
Local: Mon, Feb 8 2010 9:59 am
Subject: Re: How to create a different set of fields for a model?

OK thanks for the heads-up, I'll try using list_fields instead

--
Thibaud Morel l'Horset
http://teebes.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thibaud Morel l'Horset  
View profile  
 More options Feb 8 2010, 11:06 am
From: "Thibaud Morel l'Horset" <tee...@gmail.com>
Date: Mon, 8 Feb 2010 11:06:30 -0500
Local: Mon, Feb 8 2010 11:06 am
Subject: Re: How to create a different set of fields for a model?

Jesper,

  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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vermus  
View profile  
 More options Feb 9 2010, 3:27 am
From: Vermus <vermus.jab...@gmail.com>
Date: Tue, 9 Feb 2010 00:27:11 -0800 (PST)
Local: Tues, Feb 9 2010 3:27 am
Subject: Re: How to create a different set of fields for a model?
On 8 фев, 17:11, jespern <jno...@gmail.com> wrote:

> 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...

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vermus  
View profile  
 More options Feb 9 2010, 8:56 am
From: Vermus <vermus.jab...@gmail.com>
Date: Tue, 9 Feb 2010 05:56:36 -0800 (PST)
Local: Tues, Feb 9 2010 8:56 am
Subject: Re: How to create a different set of fields for a model?
On 9 фев, 11:27, Vermus <vermus.jab...@gmail.com> wrote:

> 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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jespern  
View profile  
 More options Feb 9 2010, 9:05 am
From: jespern <jno...@gmail.com>
Date: Tue, 9 Feb 2010 06:05:03 -0800 (PST)
Local: Tues, Feb 9 2010 9:05 am
Subject: Re: How to create a different set of fields for a model?
I can't review that without context.

It's better if you fork piston on Bitbucket, commit your changes, and
send us a link here.

Jesper

On Feb 9, 3:56 pm, Vermus <vermus.jab...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »