2011-06-13 46 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.

相关问题