1

Summary: Why shouldn't I put model, serializer and view classes for the same object in the same file in my Django Rest Framework application?

I'm building an application using the Django Rest Framework, which I'm relatively new to. I've had some exposure to Ruby on Rails so I'm familiar with the MVC idiom and its logic, and the separation of files made more sense there since the view was an HTML file with some embedded Ruby.

I am finding it a little inefficient jumping between models.py, serializers.py and views.py when adding new objects or making changes to existing ones. It feels like it would be easier to have a file per object (or group of closely related objects) with model, serializer and view classes all near each other.

For a trivial example, why do this:

# models.py
from django.db import models

class Thing(models.Model):
    name = models.CharField()

# serializers.py
from rest_framework import serializers
from models import Thing

class ThingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thing
        fields = ['id', 'name']

# views.py
from rest_framework import generics
from models import Thing
from serializers import ThingSerializer

class ThingList(generics.ListCreateAPIView):
    queryset = Thing.objects.all()
    serializer_class = ThingSerializer

when you could do this:

# thing.py
from django.db import models
from rest_framework import serializers, generics

class Thing(models.Model):
    name = models.CharField()

class ThingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Thing
        fields = ['id', 'name']

class ThingList(generics.ListCreateAPIView):
    queryset = Thing.objects.all()
    serializer_class = ThingSerializer

I'm sure this is something that people have thought about and come up with good reasons to structure a project this way, but searching on Google, StackOverflow and here I haven't managed to find anything.

1
  • I use the ASP.NET MVC stack which is organized much the same way, and have wondered the same thing myself from time to time. Basically, having everything in separate files allows for easier re-use if you are binding multiple routes to the same View, but can feel weirdly disconnected if you only ever have one route render one dedicated View.
    – Graham
    Nov 25 '19 at 21:46
2

This was a common convention at the time django was developed, followed by other frameworks of the time like Rails where you had a models directory with all your models, a controller directory with all your controllers etc so you knew where to look based on what the thing was rather than the abstract domain concept it was connected to.

You don't have to do it this way.

There are other ways to lay out a project, such as the "pod" method where you have directories that take domain concepts and group all the relevant files together, so your post model, template and controller would be in the post directory.

Others argue to completely separate your domain logic models from your framework. See Bob Martin presentation on what he calls "Clean Architecture"

https://www.youtube.com/watch?v=o_TH-Y78tt4

There is no "correct" way, although some ways work better with the framework you are using if that is the way the framework is designed to be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.