RestFul with Class Based Views best practice

45 views
Skip to first unread message

pywebdesign

unread,
Oct 29, 2013, 3:45:03 PM10/29/13
to django...@googlegroups.com
Hi,

I am trying to find/develop a best way to name and organize Views class to be consistent and flexible.

I am working on a project witch involve an model called Song. It's kind of a midi song in a database with attibutes about authors, name ... to make it simple.

Now I want web pages that permit to create a new song, edit a song, delete it or simply view it.
I also want a list page that list song by authors, by instrument (a typical search and return a list). the idea is to make the list editable via ajax. people change something on a list and it saves it to the browser when done without quitting the list. Very useful to modify many list at the same time.
There is also a page to create/upload 5 song at a time.

I was searching the best structure for my Class Based View. Right now I came up with this one:

class SongForm
def get_new_song
def post_new_song

def get_modify_song
def put_song

def get_delete_song
def delete_song

class SongListForm
def get_list_song(search infos)

class SongView
get_Song


ok that's about it. It look wrong, one big class and to very small one. I would like some pointer to some best practice to create this kind of CRUD as RESTFUL.

Thank you very much


Marc Aymerich

unread,
Oct 29, 2013, 4:18:51 PM10/29/13
to django-users
Hi, what i'd do if I want to be restful is create two views, one for listing and creating songs and another for reading, updating and deleting existing songs

class SongList
   # url /songs/
   def get: list songs
   def post: create new song

class Song
   # url /songs/<song_id>
   def get
   def put
   def patch
   def delete

I think with these two views you can cover all use-cases you've described :)

--
Marc

Xavier Ordoquy

unread,
Oct 29, 2013, 4:45:58 PM10/29/13
to django...@googlegroups.com
Hi,

You probably want to have a look at django rest framework.
You use can will be easy with it, in particular if you use http://django-rest-framework.org/api-guide/viewsets.html#modelviewset

Regards,
Xavier.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/61688c29-1d49-4d1a-9d04-45192d00c78d%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

pywebdesign

unread,
Oct 29, 2013, 6:09:37 PM10/29/13
to django...@googlegroups.com
Ok, thank you! I was not doing it correctly at all. http://django-rest-framework.org is amazing.

I am now using Marc's design and trying to event use CreateView and other generic view
Reply all
Reply to author
Forward
0 new messages