我正在尝试拆分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 https://stackoverflow.com/questions/1160579/models-py-getting-huge-what-is-the-best-way-to-break-it-up,但是在这个解决方案中我仍然有一个问题,当我运行时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;
我对此不太确定,但我担心这部分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"
那么,我应该手动运行参考更改吗?这可能会给我带来南方的问题吗?