Why does the Django Rest Framework encourage separating Model, Serializer and Views for the same object across multiple files?

softwareengineering.stackexchange https://softwareengineering.stackexchange.com/questions/401632

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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
scroll top