About 20 models in 1 django app

前端 未结 6 747
太阳男子
太阳男子 2021-01-30 07:05

I have started work on a local app for myself that runs through the browser. Having recently gone through the django tutorial I\'m thinking that it might be better to use django

6条回答
  •  一个人的身影
    2021-01-30 07:53

    This is a pretty common need... I can't imagine wading through a models.py file that's 10,000 lines long :-)

    You can split up the models.py file (and views.py too) into a pacakge. In this case, your project tree will look like:

    /my_proj
        /myapp
            /models
                __init__.py
                person.py
    

    The __init__.py file makes the folder into a package. The only gotcha is to be sure to define an inner Meta class for your models that indicate the app_label for the model, otherwise Django will have trouble building your schema:

    class Person(models.Model):
        name = models.CharField(max_length=128)
    
        class Meta:
            app_label = 'myapp'
    

    Once that's done, import the model in your __init__.py file so that Django and sync db will find it:

    from person import Person
    

    This way you can still do from myapp.models import Person

提交回复
热议问题