我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。
通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。
调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。
如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?
当前回答
另一个可能的原因是,如果您在另一个文件中定义了一些模型(而不是在包中),并且没有在其他任何地方引用它。
对我来说,简单地从.graph_model import *添加到admin.py(其中graph_model.py是新文件)就可以解决这个问题。
其他回答
这是一个评论,但可能应该是一个答案。
确保你的应用程序名称在settings.py INSTALLED_APPS中,否则无论你做什么,它都不会运行迁移。
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
然后运行:
./manage.py makemigrations blog
另一个可能的原因是,如果您在另一个文件中定义了一些模型(而不是在包中),并且没有在其他任何地方引用它。
对我来说,简单地从.graph_model import *添加到admin.py(其中graph_model.py是新文件)就可以解决这个问题。
我从django外部复制了一个表,Meta类默认为“managed = false”。例如:
class Rssemailsubscription(models.Model):
id = models.CharField(primary_key=True, max_length=36)
...
area = models.FloatField('Area (Sq. KM)', null=True)
class Meta:
managed = False
db_table = 'RSSEmailSubscription'
通过将managed改为True, makemig开始发生变化。
我这样解决了这个问题:
Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc". Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".
确保你的应用在settings.py中的installed_apps中被提及 确保建模类扩展了模型。模型