在Django应用程序中重构大型models.py文件

时间:2021-11-07 21:36:01

After reading monokrome's answer to Where should Django manager code live?, I've decided to split a large models.py into smaller, more manageable files. I'm using the folder structure

在阅读了monokrome对Django管理器代码应该在哪里的答案后,我决定将大型models.py分成更小,更易于管理的文件。我正在使用文件夹结构

foodapp/
    models/
        __init__.py #contains pizza model
        morefood.py #contains hamburger & hotdog models

In __init__.py, I import the models from morefood.py with

在__init__.py中,我从morefood.py导入模型

from morefood import hamburger, hotdog

However, when I run python manage.py syncdb, the only table created is foodapp_pizza - What do I need to do to get Django to create tables for the models I have imported from morefood.py?

但是,当我运行python manage.py syncdb时,唯一创建的表是foodapp_pizza - 我需要做些什么才能让Django为我从morefood.py导入的模型创建表?

2 个解决方案

#1


3  

Or, in your morefood.py models, add the following to the Meta:

或者,在您的morefood.py模型中,将以下内容添加到Meta:

class Meta:
    app_label = 'foodapp'

Then syncdb will work with your existing structure

然后syncdb将与您现有的结构一起使用

#2


7  

Try this:

尝试这个:

foodapp/
    __init__.py
    models.py
    /morefood
        __init__.py
        hamburger.py
        hotdog.py

and do this in models.py:

并在models.py中执行此操作:

from foodapp.morefood.hamburger import *
from foodapp.morefood.hotdog import *

as suggested in this blogpost.

正如本博文中所述。

#1


3  

Or, in your morefood.py models, add the following to the Meta:

或者,在您的morefood.py模型中,将以下内容添加到Meta:

class Meta:
    app_label = 'foodapp'

Then syncdb will work with your existing structure

然后syncdb将与您现有的结构一起使用

#2


7  

Try this:

尝试这个:

foodapp/
    __init__.py
    models.py
    /morefood
        __init__.py
        hamburger.py
        hotdog.py

and do this in models.py:

并在models.py中执行此操作:

from foodapp.morefood.hamburger import *
from foodapp.morefood.hotdog import *

as suggested in this blogpost.

正如本博文中所述。