4

I am using django-rest-framework for api for my webapp. Is it good to use django rest framework in place of default ORM provided by django? I have refered to this post and still confused. As drf-api requires classes to be created and I think its better to use that code for handling objects since I can reuse code.

urls.py

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)

views.py

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

serializers.py

class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User
        fields = ('url', 'username', 'email', 'groups')

How can I handle objects the crud way in views.py using django rest framework?

2
  • No, I don't think you should be using DRF on the server side if you have access to the model. Or is this not what you're asking? Commented Aug 19, 2015 at 5:10
  • @ArnarYngvason, your comment fits as answer to the first part of my question. Why is it bad to use drf instead of django orm? I have updated this question. Can you please refer to the link in updated question? Commented Aug 19, 2015 at 5:47

2 Answers 2

3

There are two layers here: The Model and the Model Serializer; the Model being the bottom layer.

  • When you need to interact with the database within your Django Application you should use the Model.
  • When you need to interact with the database from the client, you can either...

    • A: create a view which, works in the traditional way of rendering the content on the server side and then send back a POST request from the client to the view, if you want to edit (i.e. django-forms) , or...
    • B: Setup a REST API that lets you fetch and update your database content via AJAX request. This is the purpose of an API.
  • If you have any logic that has to run regardless of whether you're dealing with the Model or the Model Serializer, then it should be implemented on the bottom layer, i.e. the Model.

The reason we often today use an API even though we're not building an external application is that it allows for more interactive and faster user interfaces. Most popular front-end frameworks today, (for example angularjs), are built around the concept of using an API.

Sign up to request clarification or add additional context in comments.

6 Comments

Do an indirection (in this case, I think the REST Api layer) as mentioned in programmers.stackexchange post really helps with security as only the states are transferred by REST api?
Security is implemented in the view in any case (be it the ModelViewSet or a traditional django view). Neither the model or the serializer are responsible for security. You can decide to only ever interact with the database through the API [from the client], but the reasons for that should not be security, but rather software architecture.
I think you might be interpreting too much from that post you referred to. Don't over complicate things. Just use the right tool for the job.
Just to be clear though: Having an API as opposed to, for example, exposing the database, for querying, to the client will of course allow you to have more access control. In short; if you want an external client to be able to interact with the database, then you should use an API.
No, there is no advantage to using the API when you have access to the ORM. Yes, DRF is layered on top of the Django ORM, but it's specific purpose is to create an API.
|
1

I'd like to add a possible scenario to Arnar Yngvason's excellent answer.

  • C: You can use DRF's viewset from your view code.

     review_metas_response = ReviewMetaViewSet.as_view({
         'get': 'user_visit_list'
     })(request, format="json", limit=REVIEW_META_NUM)
    
  • D: You can use DRF's serializer from your view code.

     review_meta = ReviewMeta.objects.get(id=review_meta_id)
     serializer = ReviewMetaSerializer(review_meta, context=context)
    

I tend to make model thick (many methods) and make DRF serializer & viewset thick and make views thin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.