问题是,每当我尝试迁移更改时,迁移都不适用于名为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)),
],
),
]
以下是我认为您应该看到的一些图片
我不知道为什么会说创建模型userinfo,因为它已经存在
数据库截图
请任何人可以帮忙吗?
答案 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__
文件夹然后分别重新运行makemigrations
和migrate
命令