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'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'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'Horset <span dir=3D"ltr"><<a href=3D"mailto:tee...@gmail.com">te=
e...@gmail.com</a>></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'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"><<a href=3D"mailto:jnoehr@gma=
il.com" target=3D"_blank">jno...@gmail.com</a>></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's the =
fact that you're modifying 'self.fields'. 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, "Thibaud Morel l'Horset" <<a href=3D"=
mailto:tee...@gmail.com" target=3D"_blank">tee...@gmail.com</a>> wrote:<=
br>
> I do but it's a scenario where each threads dies out after a coupl=
e seconds<br>
> and is then replaced by a new one so it's possible that masks the =
side<br>
> effects... which part do you see affecting threading? is it using a li=
st<br>
> instead of a tuple that's the problem?<br>
><br>
><br>
><br>
><br>
><br>
</div><div><div></div><div>> On Mon, Feb 8, 2010 at 9:37 AM, jespern <=
;<a href=3D"mailto:jno...@gmail.com" target=3D"_blank">jno...@gmail.com</a>=
> wrote:<br>
> > On Feb 8, 4:18 pm, "Thibaud Morel l'Horset" <<a =
href=3D"mailto:tee...@gmail.com" target=3D"_blank">tee...@gmail.com</a>>=
wrote:<br>
> > > One trick I regularly use is to define fields as a list inst=
ead of the<br>
> > tuple<br>
> > > and then append to it in read(). I don't know if there a=
re issues with<br>
> > doing<br>
> > > it but so far it's worked great:<br>
><br>
> > > class PlayerHandler(BaseHandler):<br>
> > > =A0 =A0 allowed_methods =3D ('GET',)<br>
> > > =A0 =A0 model =3D Player<br>
> > > =A0 =A0 fields =3D [<br>
> > > =A0 =A0 =A0 =A0 'id',<br>
> > > =A0 =A0 =A0 =A0 'name',<br>
> > > =A0 =A0 =A0 =A0 'level',<br>
> > > =A0 =A0 =A0 =A0 'hp',<br>
> > > =A0 =A0 =A0 =A0 'max_hp',<br>
> > > =A0 =A0 ]<br>
><br>
> > > =A0 =A0 def read(self, request, id):<br>
> > > =A0 =A0 =A0 =A0 player =3D Player.objects.get(pk=3Did)<br>
><br>
> > > =A0 =A0 =A0 =A0 if player.user =3D=3D request.user:<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 self.fields +=3D [<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'builder_mode',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'items',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'mp',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'max_mp',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'experience',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'next_level',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'equipment',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'inventory',<br>
> > > =A0 =A0 =A0 =A0 =A0 =A0 ]<br>
><br>
> > > =A0 =A0 =A0 =A0 return player<br>
><br>
> > I don't think that's anywhere *near* thread-safe. Have yo=
u run this in<br>
> > a threaded environment?<br>
><br>
> > Jesper<br>
><br>
> > > On Mon, Feb 8, 2010 at 9:11 AM, jespern <<a href=3D"mailt=
o:jno...@gmail.com" target=3D"_blank">jno...@gmail.com</a>> wrote:<br>
> > > > On Feb 8, 4:08 pm, Vermus <<a href=3D"mailto:vermus.=
jab...@gmail.com" target=3D"_blank">vermus.jab...@gmail.com</a>> wrote:<=
br>
> > > > > How to create a different set of fields for a mode=
l? For example, I<br>
> > > > > want in a particular case, request a list with a r=
educed set of<br>
> > > > > fields, and in choosing from a list of elements in=
the UI, to seek a<br>
> > > > > more complete set of fields to display the object.=
At the moment, I<br>
> > > > > understand, I can not create two Handlers for one =
model (http://<br>
> > > > > <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>
> > > > > fields-from-previous-handlers). What suggestions?<=
br>
><br>
> > > > We extract the list of fields last minute, and there is=
a somewhat<br>
> > > > undocumented feature that should provide what you'r=
e after.<br>
><br>
> > > > It's called 'list_fields', and will overrid=
e 'fields', in case you<br>
> > > > return a list or a QuerySet.<br>
><br>
> > > > The feature is here:<br>
> > > ><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>
> > ..<br>
><br>
> > > > Jesper<br>
><br>
> > > > --<br>
> > > > You received this message because you are subscribed to=
the Google<br>
> > Groups<br>
> > > > "django-piston" group.<br>
> > > > 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>
> > > > To unsubscribe from this group, send email to<br>
> > > > <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>><br>
> > <django-piston%2Bunsubscribe@goog <a href=3D"http://legroups.c=
om" target=3D"_blank">legroups.com</a>><br>
> > > > .<br>
> > > > For more options, visit this group at<br>
> > > ><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>
><br>
> > > --<br>
> > > Thibaud Morel l'Horsethttp://<a href=3D"http://teebes.co=
m" target=3D"_blank">teebes.com</a><br>
><br>
> > --<br>
> > You received this message because you are subscribed to the Googl=
e Groups<br>
> > "django-piston" group.<br>
> > 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>
> > To unsubscribe from this group, send email to<br>
> > <a href=3D"mailto:django-piston%2Bunsubscribe@googlegroups.com" t=
arget=3D"_blank">django-piston+unsubscribe@googlegroups.com</a><django-p=
iston%2Bunsubscribe@goog <a href=3D"http://legroups.com" target=3D"_blank">=
legroups.com</a>><br>
> > .<br>
> > For more options, visit this group at<br>
> ><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>
><br>
> --<br>
> Thibaud Morel l'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" 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'Horset<br><a href=3D=
"http://teebes.com" target=3D"_blank">http://teebes.com</a><br>
</div></div></blockquote></div><br>
--0016364995af03f77f047f18fc4b--