python django将模型拆分为单独的文件

时间:2016-11-16 05:09:35

标签: python django

我在学习python(3.5.2)和django(1.10.3)时遇到了困难。我有一个项目,该项目有我的第一个应用程序。这是一个会员区。我预计这个应用程序将有许多模型,我喜欢将模型拆分成自己的文件的想法。我在这里看到了对此的解释:https://code.djangoproject.com/wiki/CookBookSplitModelsToFiles

    -- MyApplication
       |-- __init__.py
       |-- settings.py
       |-- urls.py
       |-- wsgi.py
    -- Members
       |-- __init__.py
       |-- admin.py
       |-- apps.py
       |-- migrations
       |   |-- __init__.py
       |-- models
       |   |-- __init__.py
       |   |-- address.py
       |-- tests.py
       |-- urls.py
       |-- views.py
    -- manage.py

Members/models/__init__.py看起来像

    from address import Address

Members/models/address.py看起来像

    from django.db import models

    class Address(models.Model):

        Member_ID = models.ForeignKey(
            settings.AUTH_USER_MODEL,
            on_delete=models.CASCADE
        )
        Order = models.PositiveIntegerField()

        ...

MyApplication/Settings.py

    INSTALLED_APPS = [
        'Members.apps.MembersConfig',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        ...

当我尝试设置数据库迁移时,我得到了

$ python manage.py makemigrations Members
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/myUser/.pyenv/versions/3.5.2/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/myUser/dev.aikidocia.org/MyApplication/Members/models/__init__.py", line 1, in <module>
    from address import Address
ImportError: No module named 'address'

我错过了什么?

1 个答案:

答案 0 :(得分:1)

正如@Evert建议的那样,使用正确的相对导入。

public class TGSSampDataController : ApiController { //according to convention-based route mapping in webapiconfig //api/{controller}/{id} should map the following to this action //GET api/TGSSampData?id=379 //GET api/TGSSampData/379 [HttpGet] public HttpResponseMessage Get(string id) { ... } }