Some Questions

86 views
Skip to first unread message

Jug

unread,
Jul 6, 2009, 7:44:29 AM7/6/09
to django-piston
Hello,

I have some general questions:

1) How can the client call custom methods in my handler class?

2) Can I use my read method for registered and anonymous or do I have
to copy it to an AnonymousHandler?

3) How can I use the validate decorator (or a normal form) but allow
clients to change only one field (i.e. sending (new) data for only one
field)?


Regards
Jug

jespern

unread,
Jul 6, 2009, 9:43:48 AM7/6/09
to django-piston
On Jul 6, 2:44 pm, Jug <j...@fantasymail.de> wrote:
> Hello,
>
> I have some general questions:
>
> 1) How can the client call custom methods in my handler class?

They can't. This is REST, not RPC. The only available methods you
have, are GET/POST/PUT and DELETE.

> 2) Can I use my read method for registered and anonymous or do I have
> to copy it to an AnonymousHandler?

Yes. Patches welcome.

> 3) How can I use the validate decorator (or a normal form) but allow
> clients to change only one field (i.e. sending (new) data for only one
> field)?

Hopefully I have a good answer for this one :-)

In piston.forms, there's a basic subclass of django.forms.ModelForm,
which has an additional method, namely 'merge_from_initial'. This
method will fill out the form based on the initial data it got, via
the initial keyword argument.

Untested code, but basically how you use it:

from piston.forms import ModelForm

class SomeForm(ModelForm):
...

@validate(SomeForm)
def update(self, request):
obj = get_object_or_404(...)
form = SomeForm(initial=obj)
form.merge_from_initial()

if form.is_valid(): # well, redundant really, since @validate
takes care of that
form.save()

That'll allow the client to only send one field, and the rest of the
values will be filled from the object.


HTH,

Jesper

jug

unread,
Jul 6, 2009, 12:40:58 PM7/6/09
to django...@googlegroups.com
jespern wrote:
> On Jul 6, 2:44 pm, Jug <j...@fantasymail.de> wrote:
>
>> Hello,
>>
>> I have some general questions:
>>
>> 1) How can the client call custom methods in my handler class?
>>
>
> They can't. This is REST, not RPC. The only available methods you
> have, are GET/POST/PUT and DELETE.
>
Oh, yes, I misread the "resource methods".

>> 2) Can I use my read method for registered and anonymous or do I have
>> to copy it to an AnonymousHandler?
>>
>
> Yes. Patches welcome.
>
Works now with another base class with resource methods etc. for both
handlers.

>> 3) How can I use the validate decorator (or a normal form) but allow
>> clients to change only one field (i.e. sending (new) data for only one
>> field)?
>>
>
> Hopefully I have a good answer for this one :-)
>
> In piston.forms, there's a basic subclass of django.forms.ModelForm,
> which has an additional method, namely 'merge_from_initial'. This
> method will fill out the form based on the initial data it got, via
> the initial keyword argument.
>
> Untested code, but basically how you use it:
>
> from piston.forms import ModelForm
>
> class SomeForm(ModelForm):
> ...
>
> @validate(SomeForm)
> def update(self, request):
> obj = get_object_or_404(...)
> form = SomeForm(initial=obj)
> form.merge_from_initial()
>
> if form.is_valid(): # well, redundant really, since @validate
> takes care of that
> form.save()
>
> That'll allow the client to only send one field, and the rest of the
> values will be filled from the object.
>
Well, that did not work. :)
First, I had to convert some data to fit my form (eg user-ids to
usernames), then
the merge_from_initial helped me to get it working manually.

You should include that to the docs. (And it must be initial=[dict] or
instance=[obj].)
> HTH,
>
> Jesper
Thanks a lot,

Jug

stephan preeker

unread,
Jul 7, 2009, 5:13:34 AM7/7/09
to django-piston


On Jul 6, 3:43 pm, jespern <jno...@gmail.com> wrote:
> On Jul 6, 2:44 pm, Jug <j...@fantasymail.de> wrote:
>
> > Hello,
>
> > I have some general questions:
>
> > 1) How can the client call custom methods in my handler class?
>
> They can't. This is REST, not RPC. The only available methods you
> have, are GET/POST/PUT and DELETE.
>
> > 2) Can I use my read method for registered and anonymous or do I have
> > to copy it to an AnonymousHandler?
>
> Yes. Patches welcome.

Since a normal Handler has an anonymous field which references to the
anonymoushandler i use that
to avoid code duplication:

class handler( .. )
...
def read(... id = id ):
self.anonymous.read( id = id )

jespern

unread,
Jul 7, 2009, 5:16:09 AM7/7/09
to django-piston
On Jul 7, 12:13 pm, stephan preeker <spree...@gmail.com> wrote:
> On Jul 6, 3:43 pm, jespern <jno...@gmail.com> wrote:
> > > 2) Can I use my read method for registered and anonymous or do I have
> > > to copy it to an AnonymousHandler?
>
> > Yes. Patches welcome.
>
> Since a normal Handler has an anonymous field which references to the
> anonymoushandler i use that
> to avoid code duplication:
>
> class handler( .. )
>    ...
>    def read(... id = id ):
>        self.anonymous.read( id = id )

That's a very neat trick! I didn't even think about that.

This definitely belongs under the Tips & Tricks section. Adding it
now.


Jesper

jug

unread,
Jul 7, 2009, 8:03:18 AM7/7/09
to django...@googlegroups.com
jespern wrote:
>> Since a normal Handler has an anonymous field which references to the
>> anonymoushandler i use that
>> to avoid code duplication:
>>
>> class handler( .. )
>> ...
>> def read(... id = id ):
>> self.anonymous.read( id = id )
>>
>
> That's a very neat trick! I didn't even think about that.
>
> This definitely belongs under the Tips & Tricks section. Adding it
> now.
Yes, but you have to return it:

def read(... id = id ):

return self.anonymous.read( id = id )

And to use the same resource methods:

class resourceMethods:
@classmethod
def myfield(cls, obj):
return obj.my_attribute


class anonymousHandler(resourceMethods, BaseHandler):
fields = (myfield, ...)
...

class handler(...):
anonymous = anonymousHandler
fields = (myfield, ...) # if its the same in both classes, also move
it to resourceMethods
...

jug

unread,
Jul 7, 2009, 6:26:14 PM7/7/09
to django...@googlegroups.com
jug schrieb:

> Yes, but you have to return it:
>
> def read(... id = id ):
> return self.anonymous.read( id = id )
And create an instance of your AnonymousHandler:

def read(... id = id ):

return self.anonymous().read( id = id )

Reply all
Reply to author
Forward
0 new messages