我試圖分裂我的應用程序的models.py
成幾個文件:拆分models.py分成幾個文件
我的第一個猜測是這樣:
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
我有一樣的東西:
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;
我不是很肯定這一點,但我很擔心一個boout部分The following references should be added but depend on non-existent tables:
這是我model1.py文件:
from django.db import models
class Store(models.Model):
class Meta:
app_label = "store"
這是我model3.py文件:
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
,如果得到了評論我試試這個,發生同樣的事情:
class Product(models.Model):
store = models.ForeignKey('store.Store')
class Meta:
app_label = "product"
所以,應該我手動運行修改引用?這可能會帶給我南方的問題嗎?
如果您嘗試從「app1.models.model1 import store'?模型3中會發生什麼? – 2011-06-14 00:31:39
此外,你可能想要檢查http://stackoverflow.com/questions/5534206/how-do-i-separate-my-models-out-in-django/5534251#5534251 – 2011-06-14 00:33:26