2011-06-13 106 views
49

我試圖分裂我的應用程序的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" 

所以,應該我手動運行修改引用?這可能會帶給我南方的問題嗎?

+0

如果您嘗試從「app1.models.model1 import store'?模型3中會發生什麼? – 2011-06-14 00:31:39

+0

此外,你可能想要檢查http://stackoverflow.com/questions/5534206/how-do-i-separate-my-models-out-in-django/5534251#5534251 – 2011-06-14 00:33:26

回答

23

我甚至無法想象爲什麼你想這樣做。但我會假設你有一個很好的理由。如果我需要某種原因這樣做,我會做到以下幾點:

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和APP2/models.py

---第二部分model3和model4 ---

這是APP1 /子模型/ model1.py文件:

from django.db import models 
class Store(models.Model): 
    class Meta: 
     app_label = "store" 

因此糾正你model3文件:

from django.db import models 
from app1.models import Store 

class Product(models.Model): 
    store = models.ForeignKey(Store) 
    class Meta: 
     app_label = "product" 

編輯,在這種情況下,再次出現的人: 退房Django的時間表,不只是這個項目的一個例子。 https://github.com/thauber/django-schedule/tree/master/schedule/models https://github.com/thauber/django-schedule/

+1

關於這個答案我得到了另一個在models2.py中產生類似'from product.models import Product':ImportError:沒有模塊從appx.models中導入 – diegueus9 2011-06-14 21:30:58

+0

導入產品 – Ted 2011-06-15 01:16:26

+29

您可以通過這種方式來維護每個文件一個類。 – worc 2013-11-25 18:53:23

11

其實我已經遇到一個教程正是你問什麼,你可以在這裏查看:

http://paltman.com/breaking-apart-models-in-django/

一個關鍵點可能是相關的 - 你可能希望使用Meta類中的db_table字段將重定位的類指向自己的表。

我可以證實這種方法是工作在Django 1.3

+0

該鏈接給出了一個404 – 2016-09-14 17:27:27

+0

看起來他把它移到http://paltman.com/breaking-apart-models-in-django/ – 2016-09-19 03:48:51

63

對於任何關於Django的1.9,現在是由框架沒有定義的類元數據的支持。

https://docs.djangoproject.com/en/1.9/topics/db/models/#organizing-models-in-a-package

The manage.py startapp command creates an application structure that includes a models.py file. If you have many models, organizing them in separate files may be useful.

To do so, create a models package. Remove models.py and create a myapp/models/ directory with an __init__.py file and the files to store your models. You must import the models in the __init__.py file.

所以,在你的情況下,結構像

app1/ 
    views.py 
    __init__.py 
    models/ 
     __init__.py 
     model1.py 
     model2.py 
app2/ 
    views.py 
    __init__.py 
    models/ 
     __init__.py 
     model3.py 
     model4.py 

你只需要做

#myproject/app1/models/__init__.py: 
from .model1 import Model1 
from .model2 import Model2 

#myproject/app2/models/__init__.py: 
from .model3 import Model3 
from .model4 import Model4 

的注意事項對進口的所有類:

Explicitly importing each model rather than using from .models import * has the advantages of not cluttering the namespace, making code more readable, and keeping code analysis tools useful.