Why should we use Django Rest Framework?

138 views
Skip to first unread message

Robert librado

unread,
Oct 2, 2015, 3:46:16 PM10/2/15
to Django users
From what ive seen when i use it its like django admin but with more features.
I also noticed that it has the serializers are those for like using it with a machine ot translatign code? 
I also have question on why its needed i understand that the api is for client and users to collect data is that the whole point for an api so that an end user could embedd info or to tap into ? 

can someone explain to to me why its needed? 

Andreas Kuhne

unread,
Oct 2, 2015, 4:50:13 PM10/2/15
to django...@googlegroups.com
Hi Robert,

The main reason for using django rest framework is if you need a backend to communicate with one or more rich frontends. An example could be if you have a website running django and you want to create a mobile application for iOS or android. You can then create an API that will communicate with your django server via the django rest framework.

There is also a vast difference between the django admin site and the django rest framework. I normally use the Django Admin site to add items to the database that are needed for my datamodel. For example user groups with permissions. The django rest framework creates a complete REST API with json or xml representations of your models. However you can also browse the API via HTML to check what it looks like and also use that as the documentation of your API. That can also be switched (I think).

Django Admin cannot be used as a REST API for you models, but you are able to add / search and delete your data.

Hope that helped a litte!

Regards,

Andréas

--
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/02262f0c-862e-454b-a70b-79fb48670efa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Oct 2, 2015, 5:29:55 PM10/2/15
to django...@googlegroups.com
From what ive seen when i use it its like django admin but with more features.

Django Admin is not an API, nor can it be used that way. If you need API functionality, you would write the interface yourself, or use a package such as DRF.

DRF and the Django Admin perform the same functionality internally, but provide two different methods of external interaction. I believe what you are referring to is the browseable API feature within DRF that may give a somewhat Django Admin-like feel. (http://www.django-rest-framework.org/topics/browsable-api/). The browseable API is designed to guide programmers to the proper use of the API, and not to be interacted with directly in production.

Django Admin is designed to provide an 'out of the box' user interface that admins and staff (humans) can use to manage models via a web browser. Computers, on the other hand, do not use a browser to communicate with other computers. DRF (and all API's in a broader sense) is designed to facilitate computer to computer communication (or more succinctly, application to application).

Here is an example query of a REST API that DRF could potentially be configured to return in JSON (assuming you had access to all of the financial data that Yahoo! does):


You can past that in your browser to see what the application would receive. Obviously this isn't very human-friendly, but to a computer it's just data and can happily munch away at it.
 
 
I also noticed that it has the serializers are those for like using it with a machine ot translatign code? 

I'm not exactly sure what you mean here, but in DRF, serializers are used to convert the data values stored in model records to formats used by API consumers (other applications) such as JSON or XML. The result is generally something that looks like the data returned from the Yahoo! URL above, or could be as simple as an HTTP return code of 200 (if no data is returned from the API call). 
 
I also have question on why its needed i understand that the api is for client and users to collect data is that the whole point for an api so that an end user could embedd info or to tap into ?

I think I covered this already, but yes, exposing an API provides a way for other application programmers to integrate data and services that your application offers into their own applications.
 

can someone explain to to me why its needed? 

I recommend you search around for an explanation of API's in general.


-James

Scot Hacker

unread,
Oct 3, 2015, 11:34:24 AM10/3/15
to Django users
Just  a quick tip: Sometimes you need a little JSON endpoint for some feature of your site or project, and a full REST framework like DRF is completely overkill. Django makes this incredibly easy, out of the box:


from django.http import JsonResponse

def some_view(request):
    my_dict
= {'foo': "bar"}  # Arbitrarily complex dictionary
   
return JsonResponse(my_dict)


And that's it - the URL for that view now emits JSON, ready for JS to consume. It's ridiculous how little code you need to make it happen.

./s


Reply all
Reply to author
Forward
0 new messages