无法在Django2中进行迁移-存在ModuleNotFoundError

时间:2019-07-08 23:27:34

标签: python-3.x django-migrations django-2.2

我是Python和django的新手,我正在尝试构建自己的Web API。我的文件夹结构如下:

|-Project
|--manage.py
|--env
|--restapi
|---api
|----migrations
|----__init__.py
|----admin.py
|----apps.py
|----models.py
|----views.py
|----tests.py
|---__init__.py
|---settings.py
|---urls.py
|---wsgi.py

我已经在settings.py的INSTALLED_APPS中添加了“ api”,但是,当我尝试进行迁移时,出现错误

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 "/Users/myname/Documents/Project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/myname/Documents/Project/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Users/myname/Documents/Project/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/myname/Documents/Project/env/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/myname/Documents/Project/env/lib/python3.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'api'

我已经在api文件夹中的admin.py中注册了模型,并且已经在settings.py中的INSTALLED_APPS中添加了“ api”模块,但是仍然出现相同的错误。

从模型.PY:

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _
from django.conf import settings```


class User(AbstractUser):
    username = models.CharField(blank=True, null=True)
    email = models.EmailField(_('email address'), unique=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'first_name', 'last_name']```

    def __str__(self):
        return '{}'.format(self.email)


class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE(), related_name='profile')
    title = models.CharField(max_length=5)
    dob = models.DateField()
    address = models.CharField(max_length=255)
    country = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    zip = models.CharField(max_length=5)
    licenses = models.ImageField(upload_to='uploads', blank=True)
    photo1 = models.ImageField(upload_to='uploads', blank=True)
    photo2 = models.ImageField(upload_to='uploads', blank=True)
    photo3 = models.ImageField(upload_to='uploads', blank=True)
    photo4 = models.ImageField(upload_to='uploads', blank=True)
    photo5 = models.ImageField(upload_to='uploads', blank=True)
    photo6 = models.ImageField(upload_to='uploads', blank=True)

从ADMIN.PY:

from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User, UserProfile


class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False


admin.site.register(User)


class UserAdmin(BaseUserAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )

    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
        }),
    )

    list_display = ('email', 'first_name', 'last_name', 'is_staff')
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
    inlines = (UserProfileInline, )

FROM SETTINGS.PY:

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

1 个答案:

答案 0 :(得分:1)

您必须在INSTALLED_APPS中写入“ restapi”,并且您的models.py文件也应位于restapi文件夹中,makemigrations命令找不到该“ api”文件夹