Django model, view split, add service

If the split is not applied, and the views.py models.py bloated files, can be resolved in the following manner, the directory structure:

 

 

 models must be a package (that is, there is __init__.py):

 

 

 __init__.py file contents:

from .gradeModel import *
from .personModel import *
from .studentModel import * 

 

Meta inner class plus a model class definition: 

from django.db import models

from app.models import Grade


class Student(models.Model):
    s_name=models.CharField(max_length=16)
    s_grade=models.ForeignKey(Grade, on_delete=models.CASCADE)

    class Meta:
        app_label = 'Student'

  

 

views also package

 

 __init__.py file contents:

from .person import *

 

 

services as a service layer, complex business logic can be placed inside, by calling view

 

 

 

Overall directory structure:

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/winstonsias/p/11547878.html