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
Message from discussion How to create a different set of fields for a model?

X-BeenThere: django-piston@googlegroups.com
Received: by 10.229.100.199 with SMTP id z7ls179371qcn.0.p; Mon, 08 Feb 2010 
	08:06:33 -0800 (PST)
Received: by 10.229.32.12 with SMTP id a12mr105358qcd.15.1265645192752;
        Mon, 08 Feb 2010 08:06:32 -0800 (PST)
Received: by 10.229.32.12 with SMTP id a12mr105357qcd.15.1265645192712;
        Mon, 08 Feb 2010 08:06:32 -0800 (PST)
Return-Path: <tee...@gmail.com>
Received: from mail-qy0-f177.google.com (mail-qy0-f177.google.com [209.85.221.177])
        by gmr-mx.google.com with ESMTP id 24si520073qyk.6.2010.02.08.08.06.31;
        Mon, 08 Feb 2010 08:06:31 -0800 (PST)
Received-SPF: pass (google.com: domain of tee...@gmail.com designates 209.85.221.177 as permitted sender) client-ip=209.85.221.177;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of tee...@gmail.com designates 209.85.221.177 as permitted sender) smtp.mail=tee...@gmail.com; dkim=pass (test mode) header...@gmail.com
Received: by mail-qy0-f177.google.com with SMTP id 7so1262819qyk.10
        for <django-piston@googlegroups.com>; Mon, 08 Feb 2010 08:06:31 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=gamma;
        h=domainkey-signature:mime-version:received:in-reply-to:references
         :date:message-id:subject:from:to:content-type;
        bh=2iMdLmGWEibgzcLEcfuTm32oSEIBz+nDGMRXQa81Oho=;
        b=wDXD/+mBaIniuuHPJmx3vVLJSTLHRGMv9ThJ1lEVbhHN8az9SoM9MuAgjAlmWqQddo
         +0q+LP7kKVbzSDvk+PBFr0PLqrcCZ6Tuh4mA1LwcehJAUTrs+/4e6re6SaudatGRMUni
         krlbuACbrFouYD2w6/J2XLQDr0eeGO4Fb6nzI=
DomainKey-Signature: a=rsa-sha1; c=nofws;
        d=gmail.com; s=gamma;
        h=mime-version:in-reply-to:references:date:message-id:subject:from:to
         :content-type;
        b=C/gNX5HSw3nucaVXEUgHAdzDp7bnvHwewhiXsjSAQynoR3fbyGA/OLJ2mmRUXF9DZA
         SFCd+q96AkZD+95CEDJOSbT04Q2KgspbD6rih+TvvBXwMXuGbll0CMWlPqxNYParXq3E
         VY4YG7+NUR9U+iJHSucDwODHyRaTKwDaSIS4U=
MIME-Version: 1.0
Received: by 10.102.16.13 with SMTP id 13mr4666347mup.62.1265645190903; Mon, 
	08 Feb 2010 08:06:30 -0800 (PST)
In-Reply-To: <b95bb6ed1002080659g77d071cv71a77ea4d988e...@mail.gmail.com>
References: <355ad3a2-4398-4f83-b38e-293ff1d03...@r24g2000yqd.googlegroups.com>
	 <41b5a4a3-b864-4a5c-b7d7-52ddb44d7...@d37g2000yqa.googlegroups.com>
	 <b95bb6ed1002080618g3079ab10ldb86e54458d67...@mail.gmail.com>
	 <58e0fe04-4bc7-4f6a-8f1e-2366fbeb9...@m31g2000yqd.googlegroups.com>
	 <b95bb6ed1002080654l7f9af51ci2f0ba4a4c95af...@mail.gmail.com>
	 <06d73d97-607c-4120-b895-48f61659e...@36g2000yqu.googlegroups.com>
	 <b95bb6ed1002080659g77d071cv71a77ea4d988e...@mail.gmail.com>
Date: Mon, 8 Feb 2010 11:06:30 -0500
Message-ID: <b95bb6ed1002080806o7debce01hb142ff66b5cd6...@mail.gmail.com>
Subject: Re: How to create a different set of fields for a model?
From: "Thibaud Morel l'Horset" <tee...@gmail.com>
To: django-piston@googlegroups.com
Content-Type: multipart/alternative; boundary=0016364995af03f77f047f18fc4b

--0016364995af03f77f047f18fc4b
Content-Type: text/plain; charset=ISO-8859-1

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:

> 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:
>> >
>> > > > 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
>> >
>> > > 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-piston@googlegroups.com.
>> > > > > To unsubscribe from this group, send email to
>> > > > > django-piston+unsubscribe@googlegroups.com<django-piston%2Bunsubscribe@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.
>> >
>> > > > --
>> > > > Thibaud Morel l'Horsethttp://teebes.com
>> >
>> > > --
>> > > 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@googlegroups.com>
>> <django-piston%2Bunsubscribe@goog legroups.com>
>> > > .
>> > > For more options, visit this group at
>> > >http://groups.google.com/group/django-piston?hl=en.
>> >
>> > --
>> > Thibaud Morel l'Horsethttp://teebes.com
>>
>> --
>> 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@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/django-piston?hl=en.
>>
>>
>
>
> --
> Thibaud Morel l'Horset
> http://teebes.com
>

--0016364995af03f77f047f18fc4b
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Jesper,<br>
<br>
=A0 Sorry about the confusion, I realize now I had misunderstood the
original question, as well the proposed solution. I&#39;ve played with list=
_fields and I understand its use case and how it answers the question. That=
 is definitely a nice feature.<br><br>=A0 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 resou=
rce if it matches a certain criteria. For example, one resource could retur=
n 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?<br>
<br>=A0 I understand that changing the fields after the handler is instanci=
ated is problematic, but then I don&#39;t see a clear way to do that would =
be? any thoughts would be appreciated, and very sorry about all the added n=
oise.<br>
<br><div class=3D"gmail_quote">On Mon, Feb 8, 2010 at 9:59 AM, Thibaud More=
l l&#39;Horset <span dir=3D"ltr">&lt;<a href=3D"mailto:tee...@gmail.com">te=
e...@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" s=
tyle=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8e=
x; padding-left: 1ex;">
OK thanks for the heads-up, I&#39;ll try using list_fields instead<div><div=
></div><div class=3D"h5"><br><br><div class=3D"gmail_quote">On Mon, Feb 8, =
2010 at 9:57 AM, jespern <span dir=3D"ltr">&lt;<a href=3D"mailto:jnoehr@gma=
il.com" target=3D"_blank">jno...@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, =
204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">No, it&#39;s the =
fact that you&#39;re modifying &#39;self.fields&#39;. The handler is<br>
only instantiated once (per interpreter), so the next request being<br>
passed through this handler will likely suffer the side effects.<br>
<br>
<br>
Jesper<br>
<div><br>
On Feb 8, 4:54=A0pm, &quot;Thibaud Morel l&#39;Horset&quot; &lt;<a href=3D"=
mailto:tee...@gmail.com" target=3D"_blank">tee...@gmail.com</a>&gt; wrote:<=
br>
&gt; I do but it&#39;s a scenario where each threads dies out after a coupl=
e seconds<br>
&gt; and is then replaced by a new one so it&#39;s possible that masks the =
side<br>
&gt; effects... which part do you see affecting threading? is it using a li=
st<br>
&gt; instead of a tuple that&#39;s the problem?<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div><div><div></div><div>&gt; On Mon, Feb 8, 2010 at 9:37 AM, jespern &lt=
;<a href=3D"mailto:jno...@gmail.com" target=3D"_blank">jno...@gmail.com</a>=
&gt; wrote:<br>
&gt; &gt; On Feb 8, 4:18 pm, &quot;Thibaud Morel l&#39;Horset&quot; &lt;<a =
href=3D"mailto:tee...@gmail.com" target=3D"_blank">tee...@gmail.com</a>&gt;=
 wrote:<br>
&gt; &gt; &gt; One trick I regularly use is to define fields as a list inst=
ead of the<br>
&gt; &gt; tuple<br>
&gt; &gt; &gt; and then append to it in read(). I don&#39;t know if there a=
re issues with<br>
&gt; &gt; doing<br>
&gt; &gt; &gt; it but so far it&#39;s worked great:<br>
&gt;<br>
&gt; &gt; &gt; class PlayerHandler(BaseHandler):<br>
&gt; &gt; &gt; =A0 =A0 allowed_methods =3D (&#39;GET&#39;,)<br>
&gt; &gt; &gt; =A0 =A0 model =3D Player<br>
&gt; &gt; &gt; =A0 =A0 fields =3D [<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 &#39;id&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 &#39;name&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 &#39;level&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 &#39;hp&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 &#39;max_hp&#39;,<br>
&gt; &gt; &gt; =A0 =A0 ]<br>
&gt;<br>
&gt; &gt; &gt; =A0 =A0 def read(self, request, id):<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 player =3D Player.objects.get(pk=3Did)<br>
&gt;<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 if player.user =3D=3D request.user:<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 self.fields +=3D [<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;builder_mode&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;items&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;mp&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;max_mp&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;experience&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;next_level&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;equipment&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 &#39;inventory&#39;,<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 =A0 =A0 ]<br>
&gt;<br>
&gt; &gt; &gt; =A0 =A0 =A0 =A0 return player<br>
&gt;<br>
&gt; &gt; I don&#39;t think that&#39;s anywhere *near* thread-safe. Have yo=
u run this in<br>
&gt; &gt; a threaded environment?<br>
&gt;<br>
&gt; &gt; Jesper<br>
&gt;<br>
&gt; &gt; &gt; On Mon, Feb 8, 2010 at 9:11 AM, jespern &lt;<a href=3D"mailt=
o:jno...@gmail.com" target=3D"_blank">jno...@gmail.com</a>&gt; wrote:<br>
&gt; &gt; &gt; &gt; On Feb 8, 4:08 pm, Vermus &lt;<a href=3D"mailto:vermus.=
jab...@gmail.com" target=3D"_blank">vermus.jab...@gmail.com</a>&gt; wrote:<=
br>
&gt; &gt; &gt; &gt; &gt; How to create a different set of fields for a mode=
l? For example, I<br>
&gt; &gt; &gt; &gt; &gt; want in a particular case, request a list with a r=
educed set of<br>
&gt; &gt; &gt; &gt; &gt; fields, and in choosing from a list of elements in=
 the UI, to seek a<br>
&gt; &gt; &gt; &gt; &gt; more complete set of fields to display the object.=
 At the moment, I<br>
&gt; &gt; &gt; &gt; &gt; understand, I can not create two Handlers for one =
model (http://<br>
&gt; &gt; &gt; &gt; &gt; <a href=3D"http://bitbucket.org/jespern/django-pis=
ton/wiki/FAQ#why-does-piston-use-" target=3D"_blank">bitbucket.org/jespern/=
django-piston/wiki/FAQ#why-does-piston-use-</a><br>
&gt; &gt; &gt; &gt; &gt; fields-from-previous-handlers). What suggestions?<=
br>
&gt;<br>
&gt; &gt; &gt; &gt; We extract the list of fields last minute, and there is=
 a somewhat<br>
&gt; &gt; &gt; &gt; undocumented feature that should provide what you&#39;r=
e after.<br>
&gt;<br>
&gt; &gt; &gt; &gt; It&#39;s called &#39;list_fields&#39;, and will overrid=
e &#39;fields&#39;, in case you<br>
&gt; &gt; &gt; &gt; return a list or a QuerySet.<br>
&gt;<br>
&gt; &gt; &gt; &gt; The feature is here:<br>
&gt; &gt; &gt; &gt;<a href=3D"http://bitbucket.org/jespern/django-piston/sr=
c/tip/piston/resource.py" target=3D"_blank">http://bitbucket.org/jespern/dj=
ango-piston/src/tip/piston/resource.py</a>.<br>
&gt; &gt; ..<br>
&gt;<br>
&gt; &gt; &gt; &gt; Jesper<br>
&gt;<br>
&gt; &gt; &gt; &gt; --<br>
&gt; &gt; &gt; &gt; You received this message because you are subscribed to=
 the Google<br>
&gt; &gt; Groups<br>
&gt; &gt; &gt; &gt; &quot;django-piston&quot; group.<br>
&gt; &gt; &gt; &gt; To post to this group, send email to <a href=3D"mailto:=
django-piston@googlegroups.com" target=3D"_blank">django-piston@googlegroup=
s.com</a>.<br>
&gt; &gt; &gt; &gt; To unsubscribe from this group, send email to<br>
&gt; &gt; &gt; &gt; <a href=3D"mailto:django-piston%2Bunsubscribe@googlegro=
ups.com" target=3D"_blank">django-piston+unsubscribe@googlegroups.com</a>&l=
t;django-piston%2Bunsubscribe@goog <a href=3D"http://legroups.com" target=
=3D"_blank">legroups.com</a>&gt;<br>


&gt; &gt; &lt;django-piston%2Bunsubscribe@goog <a href=3D"http://legroups.c=
om" target=3D"_blank">legroups.com</a>&gt;<br>
&gt; &gt; &gt; &gt; .<br>
&gt; &gt; &gt; &gt; For more options, visit this group at<br>
&gt; &gt; &gt; &gt;<a href=3D"http://groups.google.com/group/django-piston?=
hl=3Den" target=3D"_blank">http://groups.google.com/group/django-piston?hl=
=3Den</a>.<br>
&gt;<br>
&gt; &gt; &gt; --<br>
&gt; &gt; &gt; Thibaud Morel l&#39;Horsethttp://<a href=3D"http://teebes.co=
m" target=3D"_blank">teebes.com</a><br>
&gt;<br>
&gt; &gt; --<br>
&gt; &gt; You received this message because you are subscribed to the Googl=
e Groups<br>
&gt; &gt; &quot;django-piston&quot; group.<br>
&gt; &gt; To post to this group, send email to <a href=3D"mailto:django-pis=
ton@googlegroups.com" target=3D"_blank">django-piston@googlegroups.com</a>.=
<br>
&gt; &gt; To unsubscribe from this group, send email to<br>
&gt; &gt; <a href=3D"mailto:django-piston%2Bunsubscribe@googlegroups.com" t=
arget=3D"_blank">django-piston+unsubscribe@googlegroups.com</a>&lt;django-p=
iston%2Bunsubscribe@goog <a href=3D"http://legroups.com" target=3D"_blank">=
legroups.com</a>&gt;<br>


&gt; &gt; .<br>
&gt; &gt; For more options, visit this group at<br>
&gt; &gt;<a href=3D"http://groups.google.com/group/django-piston?hl=3Den" t=
arget=3D"_blank">http://groups.google.com/group/django-piston?hl=3Den</a>.<=
br>
&gt;<br>
&gt; --<br>
&gt; Thibaud Morel l&#39;Horsethttp://<a href=3D"http://teebes.com" target=
=3D"_blank">teebes.com</a><br>
<br>
--<br>
You received this message because you are subscribed to the Google Groups &=
quot;django-piston&quot; group.<br>
To post to this group, send email to <a href=3D"mailto:django-piston@google=
groups.com" target=3D"_blank">django-piston@googlegroups.com</a>.<br>
To unsubscribe from this group, send email to <a href=3D"mailto:django-pist=
on%2Bunsubscribe@googlegroups.com" target=3D"_blank">django-piston+unsubscr=
ibe@googlegroups.com</a>.<br>
For more options, visit this group at <a href=3D"http://groups.google.com/g=
roup/django-piston?hl=3Den" target=3D"_blank">http://groups.google.com/grou=
p/django-piston?hl=3Den</a>.<br>
<br>
</div></div></blockquote></div><br><br clear=3D"all"><br></div></div><div><=
div></div><div class=3D"h5">-- <br>Thibaud Morel l&#39;Horset<br><a href=3D=
"http://teebes.com" target=3D"_blank">http://teebes.com</a><br>
</div></div></blockquote></div><br>

--0016364995af03f77f047f18fc4b--