迁移不适用于特定模型

时间:2020-09-27 16:30:08

标签: python django

问题是,每当我尝试迁移更改时,迁移都不适用于名为Userinfo的特定应用。我的终端中的信使是

Operations to perform:
  Apply all migrations: admin, auth, books_details, contenttypes, sessions
Running migrations:
  No migrations to apply.

该应用未列在我的上面列表中,我也不知道是什么原因

该应用程序的Models.py

from django.db import models
import os
# from django.conf import settings
def upload_rename(instance,filename):
    exe=filename.split('.')[-1]
    filename=instance.user_name+'.'+exe
    try:
        os.remove(os.path.join('images','profile_image',instance.user_name,filename))
    except:
        pass
    return os.path.join('profile_image',instance.user_name,filename)

class Userinfo(models.Model):
    ''' User info '''
    user_name = models.CharField(max_length=30, unique=True, null=False,primary_key=True)
    full_name = models.CharField(max_length=50, null=False)
    user_email = models.EmailField(max_length=254)
    college_name = models.CharField(max_length=50)
    city = models.CharField(max_length=50)
    country = models.CharField(max_length=50)
    profile_img = models.ImageField(upload_to=upload_rename, blank=True)
    ''' The Change I added '''
    varified_user =models.BooleanField(default=False)

该应用的Admin.py

from django.contrib import admin
from .models import Userinfo
admin.site.register(Userinfo)

setting.py中的INSTALLED_APP

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'userinfo',
    'rest_framework',
    'phonenumber_field',
    'books_details',
]

迁移

from django.db import migrations, models
import userinfo.models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Userinfo',
            fields=[
                ('user_name', models.CharField(max_length=30, primary_key=True, serialize=False, unique=True)),
                ('full_name', models.CharField(max_length=50)),
                ('user_email', models.EmailField(max_length=254)),
                ('college_name', models.CharField(max_length=50)),
                ('city', models.CharField(max_length=50)),
                ('country', models.CharField(max_length=50)),
                ('profile_img', models.ImageField(blank=True, upload_to=userinfo.models.upload_rename)),
                ('varified_user', models.BooleanField(default=False)),
            ],
        ),
    ]

以下是我认为您应该看到的一些图片 Terminal Message when i try to migrate
我不知道为什么会说创建模型userinfo,因为它已经存在
数据库截图 Screenshot of my database where i want to apply changes 请任何人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我想您需要先运行makemigrations命令:

python manage.py makemigrations userinfo

,然后运行migrate命令:

python manage.py migrate

请参阅django docx的本教程:https://docs.djangoproject.com/en/3.1/intro/tutorial02/#activating-models,以获得更多说明

通过运行 makemigrations ,您告诉Django您对模型进行了一些更改(在这种情况下,您进行了新的更改),并且您希望对作为迁移存储。

更新

在某些情况下,最好是从零开始重新启动迁移过程,以解决这种情况 因此,由于您处于开发模式,我建议您删除:

  • 数据库(或更好地进行备份)
  • 为每个应用迁移migrations文件夹下的文件
  • 不要忘记__pycache__文件夹

然后分别重新运行makemigrationsmigrate命令

相关问题