新创建的Django项目不会生成迁移

时间:2019-11-08 06:45:17

标签: django

我试图添加一个简单的模型并在新创建的Django应用(2.2.7)中运行迁移。因为我是Django的新手,所以我似乎错过了一些重要的设置。我在下面列出了我已完成的步骤,包括底部的错误消息。

django-admin startproject myproject
cd myproject
django-admin startapp myapp

这是当前的项目结构

➜  myproject tree
.
├── manage.py
├── myapp
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── myproject
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

这是manage.py文件:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

我将应用myapp添加到myproject/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myproject.apps.MyappConfig'
]

我在myapp/models.py中添加了一个模型:

from django.db import models


class Post(models.Post):
    title = models.CharField(max_length=100)

现在,我想基于模型创建迁移:

python3 manage.py makemigrations

我得到的是这个错误:

➜  myproject python3 manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
    self.check()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 35, in check_url_namespaces_unique
    if not getattr(settings, 'ROOT_URLCONF', None):
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting ROOT_URLCONF, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

据我了解,DJANGO_SETTINGS_MODULE应该在manage.py文件中设置。我想念什么?预先感谢!

2 个答案:

答案 0 :(得分:3)

也许这是一个问题,但是在您已安装的应用中...

INSTALLED_APPS = [
    ...,
    'myproject.apps.MyappConfig'
]

根据您的项目树,应该

INSTALLED_APPS = [
    ...,
    'myapp.apps.MyappConfig'
]

答案 1 :(得分:0)

Kindly activate your virtual environment

Add 'myapp' in settings.py  - installed_app

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp'
]

then execute:-
python3 manage.py makemigrations