using the django rest framework properly

Here is one of the functions I have using the django rest framework:

elif request.method == 'POST':
    text = request.POST.get('text')
    request_to_meet = request.POST.get('request')
    startup = request.POST.get('startup')
    to_user_id = request.POST.get('tid')

    try:
        to_user = User.objects.get(pk=to_user_id)
    except User.DoesNotExist:
        to_user = None

    if 'conversation_id' in request.POST:
        conversation_id = request.POST.get('conversation_id')
    else:
        conversation_id = 0

    ct = Chat.objects.create(
        from_user=user,
        to_user=to_user,
        text=text,
        startup=startup,
        request=request_to_meet,
        conversation_id=conversation_id)

    serializer = ChatSerializer(ct, many=False)
    return Response(serializer.data)

Am I using the framework properly? I had to use the create( model function in django instead of using the serializer save function. Is that the proper way to right api code? Thanks!

Answers


Let me preface by saying, I am new to django rest framework too.

The main things I see are:

  1. Are you using api_view decorator, which provides the free web based documentation of your api methods

  2. Using request.DATA will automatically map requests with JSON in the body and content type application/json to variables.

I have yet to use serializers to create data instances also, but doing so should automatically return appropriate errors based on incorrect datatypes/missing fields, etc, opposed to you having to manually handle all your validation and object creation yourself.


First of all; if it works and the code you produce is easy to read and maintain, anything else is a bonus.

So, one thing I have learnt from Django Rest Framework is they have about a million ways to do anything, each with varying verbosity/control.

It is hard to say exactly whether or not the way you are doing things is optimal with regards to what DRF already has baked in for you as you haven't given much detail of exactly what it is you want to do in this function. However at first glance it looks like you are doing a lot of boilerplate that DRF likely already does for you.

Have you had a look through their tutorial? It is pretty good, if a bit long winded. I found that almost all of the code they tell you to write at the beginning is later replaced with much more elegant code they tell you about at the end (annoyingly).

If you can give a few more details of your exact aim (e.g. this function should do x if it is request of type y, or else blah blah) then I can give you a few examples of how I would do it in DRF.

P.S.

You can rewrite this bit:

if 'conversation_id' in request.POST:
    conversation_id = request.POST.get('conversation_id')
else:
    conversation_id = 0

into this:

conversation_id = request.POST.get('conversation_id', 0)

and it will do exactly the same thing.


Need Your Help

Problem with id column and JPA

java hibernate jpa playframework

I'm trying JPA with a very simple class for the Play! framework and I'm having some problems with the id column.

Customized Windows Save Dialog is no Longer Fancy — Why?

c++ winapi customization savefiledialog ofnhookproc

In accordance with this question I am customizing a Win32 Save File dialog with a custom template description. Now I have a problem where the Save File dialog doesn't show the left-hand bar with my

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.