将models.py拆分成几个文件 [英] Split models.py into several files
问题描述
我正在尝试将我的应用程序的 models.py
拆分为多个文件:
I'm trying to split the models.py
of my app into several files:
我的第一个猜测是这样做:
My first guess was do this:
myproject/
settings.py
manage.py
urls.py
__init__.py
app1/
views.py
__init__.py
models/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models/
__init__.py
model3.py
model4.py
这不起作用,然后我发现 this,但在这个解决方案中我仍然有问题,当我运行 python manage.py sqlall app1
我得到类似的东西:
This doesn't work, then i found this, but in this solution i still have a problem, when i run python manage.py sqlall app1
I got something like:
BEGIN;
CREATE TABLE "product_product" (
"id" serial NOT NULL PRIMARY KEY,
"store_id" integer NOT NULL
)
;
-- The following references should be added but depend on non-existent tables:
-- ALTER TABLE "product_product" ADD CONSTRAINT "store_id_refs_id_3e117eef" FOREIGN KEY ("store_id") REFERENCES "store_store" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "product_product_store_id" ON "product_product" ("store_id");
COMMIT;
我对此不太确定,但我很担心应该添加以下引用,但依赖于不存在的表:
I'm not pretty sure about this, but i'm worried aboout the part The following references should be added but depend on non-existent tables:
这是我的model1.py文件:
This is my model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
这是我的model3.py文件:
This is my model3.py file:
from django.db import models
from store.models import Store
class Product(models.Model):
store = models.ForeignKey(Store)
class Meta:
app_label = "product"
显然有效,但我在 alter table
中得到了评论,如果我尝试这样做,也会发生同样的事情:
And apparently works but i got the comment in alter table
and if I try this, same thing happens:
class Product(models.Model):
store = models.ForeignKey('store.Store')
class Meta:
app_label = "product"
那么,我应该手动运行alter for references吗?这可能会给我带来南方的问题吗?
So, should I run the alter for references manually? this may bring me problems with south?
推荐答案
我会做以下事情:
myproject/
...
app1/
views.py
__init__.py
models.py
submodels/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models.py
submodels/
__init__.py
model3.py
model4.py
然后
#myproject/app1/models.py:
from submodels/model1.py import *
from submodels/model2.py import *
#myproject/app2/models.py:
from submodels/model3.py import *
from submodels/model4.py import *
但是,如果你没有充分的理由,将model1和model2直接放在app1/models.py中,将model3和model4放在app2/models.py中
But, if you don't have a good reason, put model1 and model2 directly in app1/models.py and model3 and model4 in app2/models.py
---第二部分---
这是 app1/submodels/model1.py 文件:
This is app1/submodels/model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
因此更正您的 model3.py 文件:
Thus correct your model3.py file:
from django.db import models
from app1.models import Store
class Product(models.Model):
store = models.ForeignKey(Store)
class Meta:
app_label = "product"
已编辑,以防有人再次出现这种情况:查看 django-schedule 以获取执行此操作的项目示例.https://github.com/thauber/django-schedule/tree/master/时间表/模型https://github.com/thauber/django-schedule/
Edited, in case this comes up again for someone: Check out django-schedule for an example of a project that does just this. https://github.com/thauber/django-schedule/tree/master/schedule/models https://github.com/thauber/django-schedule/
这篇关于将models.py拆分成几个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!