user5721897
user5721897

Reputation:

Should I use both Django views and Rest Framework together and how?

I will create a web app that mostly depends on basic CRUD operations but may change in the future (like getting data from another web service). I also will create a mobile app for my web app as well in order to do the same CRUD ops and the mobile app will use the REST API of my web app.

So the question is that which way should I follow to accomplish these plans? Should I first create the REST API and then use it in my regular Django templates? Or should I create both the Django template views (CBVs, i.e. CreateView, UpdateView, DeleteView, ListView) and also REST API views separately?

I really confused as I don't know what people do for such apps. Can you help me to have a roadmap? What would you do and why?

Upvotes: 3

Views: 1254

Answers (1)

RHSmith159
RHSmith159

Reputation: 1592

I'd probably just create an api app, which just has DRF views, serializers, routers, permission classes etc (just holds all the rest framework objects). Your other apps would be normal django apps holding your models CBVs forms etc.

Then for example, when you're creating serializers, you'd just have from myapp.models import MyModel to reference the app specific models.

I've used this structure before and it seemed to work pretty well.

api
 - serializers.py
 - viewsets.py
 - custom_permissions.py
 - urls.py
 - routers.py
app_1
 - views.py
 - models.py
 - forms.py
 - tests
     - test_models.py
     - test_views.py
 - urls.py
app_2
 - views.py
 - models.py
 - forms.py
 - tests
     - test_models.py
     - test_views.py
 - urls.py

manage.py
etc etc

Upvotes: 1

Related Questions