如何在Django的其他项目中导入模型

时间:2020-12-31 11:24:16

Is it possible to import models from apps in different Django projects?

可以从不同的Django项目中导入模型吗?

I hope to move some common models in a base projects from which every child projects can share the same data in these common models.

我希望在基础项目中移动一些通用模型,每个子项目都可以从这些通用模型*享相同的数据。

Edit

编辑

I have to place

我要的地方

from baseproject.appname.models import basemodel

before

之前

os.environ['DJANGO_SETTINGS_MODULE'] = 'childproject.settings'
from django.conf import settings

in child project to access the data in base model correctly.

在子项目中,正确地访问基模型中的数据。

1 个解决方案

#1


2  

Yes. You can turn a project-specific app into a standard Python package by moving it to site-packages (or wherever your Python install expects its modules) and breaking any links from it to other apps in the project. You can then import it as you would any Python module, in any project.

是的。您可以将一个特定于项目的应用程序转换为标准的Python包,将其移动到站点包(或者您的Python安装所期望的模块),并将它与项目中的其他应用程序断开连接。然后,您可以像导入任何Python模块一样在任何项目中导入它。

#1


2  

Yes. You can turn a project-specific app into a standard Python package by moving it to site-packages (or wherever your Python install expects its modules) and breaking any links from it to other apps in the project. You can then import it as you would any Python module, in any project.

是的。您可以将一个特定于项目的应用程序转换为标准的Python包,将其移动到站点包(或者您的Python安装所期望的模块),并将它与项目中的其他应用程序断开连接。然后,您可以像导入任何Python模块一样在任何项目中导入它。