将models.py分解成多个文件 [英] Split models.py into several files
问题描述
我试图将我的应用程序的 models.py
分割成几个文件:
我的第一个猜测是这样做的:
myproject /
settings.py
manage.py
urls .py
__init__.py
app1 /
view.py
__init__.py
models /
__init__.py
model1.py
model2.py
app2 /
view.py
__init__.py
models /
__init__.py
model3.py
model4.py
这不起作用,于是我发现这个,但在这个解决方案我仍然有一个问题,当我运行 python manage.py sqlall app1
我有一些像:
BEGIN;
CREATE TABLEproduct_product(
idserial NOT NULL PRIMARY KEY,
store_idinteger NOT NULL
)
;
- 应添加以下引用,但取决于不存在的表:
- ALTER TABLEproduct_productADD CONSTRAINTstore_id_refs_id_3e117eefFOREIGN KEY(store_id)参考store_store(id )DEFERRABLE INITIALLY DEFERRED;
CREATE INDEXproduct_product_store_idONproduct_product(store_id);
COMMIT;
我不太清楚这一点,但我担心的部分应添加以下引用,但依赖于不存在的表:
这是我的model1.py文件:
从$ d code $ d $ d
class Store(models.Model):
class Meta:
app_label =store
这是我的model3.py文件:
从django.db导入模型
从store.models import Store
class Product
store = models.ForeignKey(Store)
class Meta:
app_label =product
并且显然有效,但是我在 alter table
中得到了评论,如果我尝试这样的话,会发生同样的事情:
class Product(models.Model):
store = models.ForeignKey('store.Store')
class Meta :
app_label =product
那么我应该手动运行参考的参考这可能会给我带来南方的问题?
我甚至不能想象为什么你会这样做。但我会假设你有一个很好的理由。如果我因为某些原因需要这样做,我会执行以下操作:
myproject /
...
app1 /
view.py
__init__.py
models.py
submodels /
__init__.py
model1.py
model2.py
app2 /
view.py
__init__.py
models.py
submodels /
__init__.py
model3。 py
model4.py
然后
来自子模型/ model1.py的$ m $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
#myproject / app2 / models.py:
from submodels / model3.py import *
from submodels / model4.py import *
$但是,如果没有足够的理由,将model1和model2直接放在app2 / models.py中的app1 / models.py和model3以及model4中。 p>
---第二部分---
这是app1 / submodels / model1.py文件:
从django.db导入模型
class Store(models.Model):
class Meta:
app_label =store
因此更正您的model3文件:
从django.db导入模型
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 / schedule / models
https://github.com/ thauber / django-schedule /
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 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:
This is my model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
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"
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"
So, should I run the alter for references manually? this may bring me problems with south?
I can't even begin to imagine why you'd want to do this. But I'll assume you've got a good reason. If I needed to do this for some reason, I'd do the following:
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
Then
#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 *
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
---second part---
This is app1/submodels/model1.py file:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
Thus correct your model3 file:
from django.db import models
from app1.models import Store
class Product(models.Model):
store = models.ForeignKey(Store)
class Meta:
app_label = "product"
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屋!