method modeling/calling in django-rest-framework

324 views
Skip to first unread message

John Gathm

unread,
Apr 28, 2012, 3:21:13 AM4/28/12
to django-rest-framework
Hi

I'm having difficulties figuring how to use django-rest-framork
achieving my goals, though it might be to my understanding of REST

Let's suppose I have a pseudo-model

class User :
-username
-password
-adress
-is3rpartyServerAccountActive

I understand perfectly well how I can use REST with django-rest-
framework to get a list of users, update adress if a user wants to
update his profile, etc.

However, this model is used to manage 3rd party software, IE if a user
enable or disable is3rpartyServerAccountActive, it should enable
business server logic, business script (for instance, to disable or
enable account in 3rd party software, or to take any action).
While it feels natural for me to figure how to make this in an RPC
form, I don't get it where I should hook such business logic with
django-rest-framework
where should I hook a enable3rdPartyServerAccount() function inside
django, before the model is actually updated ? Taking into account
that the function might also return false and fail.
Using pure django, I would do this logic in the view, before rendering
response, but I don't see where with django-rest-framework.

Perhaps the problem is I want to use REST to manage a not-so stateless
remote server resource ?

Regards,
J.

Marko Tibold

unread,
Apr 28, 2012, 9:44:05 AM4/28/12
to django-res...@googlegroups.com
Hi John,


Maybe you can put your business logic before the model is actually saved. You custom save method might then raise an exception of your choice if it wasn't able to change the state of your third party resource.

Marko

Tom Christie

unread,
Apr 28, 2012, 12:05:09 PM4/28/12
to django-res...@googlegroups.com
Like Marko says, one approach would be override the model save() method.
The other way round would be to write your own .put() method on the view, rather than relying on the generic behavior.
It sounds like you'd want to subclass InstanceModelView, with your own custom view.

Take a look at the existing behavior of the put method here...


You could replicate most of that, and put in your custom behavior before model_instance.save() gets called.

Vitor M. A. da Cruz

unread,
Apr 28, 2012, 12:24:30 PM4/28/12
to Tom Christie, django-res...@googlegroups.com
   You could also treat the 3rd party as a resource related to the user, and implement their operations on that resource.

De: Tom Christie
Enviada em: 28/04/2012 13:05
Para: django-res...@googlegroups.com
Assunto: Re: method modeling/calling in django-rest-framework

Reply all
Reply to author
Forward
0 new messages