问题描述
我将一个字段从CharField更改为ForeignKey,名为Availability,当我尝试迁移时,我会收到以下错误:
I changed a field from CharField to ForeignKey on a Model called Availability, when I am trying to migrate I keep getting the error below:
ValueError: Lookup failed for model referenced by field reservation.Availability.location: useraccount.Location
任何想法为什么会发生这种情况?
Any idea why this could be happening?
谢谢
--------------更新代码--- -----------
--------------UPDATED CODE--------------
应用程序:预订
from useraccount.models import Location
class Availability(models.Model):
location = models.ForeignKey(Location)
应用:useraccount
App: useraccount
class Location(models.Model):
town = models.CharField(max_length=100)
county = models.CharField(max_length=100)
def __str__(self):
return self.town + ', ' + self.county
Stacktrace
Stacktrace
系统检查确定了一些问题:
System check identified some issues:
警告:
通知.NoticeSettingsend:(1_6.W002)BooleanField没有默认值。
提示:Django 1.6将BooleanField的默认值从False更改为无。请参阅获取更多信息。
要执行的操作:
应用所有迁移:reservation
运行迁移:
应用reservation.0010_auto_20141210_0357 ...追溯(最近的最后一次呼叫):
文件/用户/ chirdeeptomar / envs / mydocbook / lib / python3.4 / site-packages / django / apps / registry.py,第148行,get_app_config
return self.app_configs [app_label]
KeyError: useraccount'
WARNINGS:notification.NoticeSetting.send: (1_6.W002) BooleanField does not have a default value. HINT: Django 1.6 changed the default value of BooleanField from False to None. See https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield for more information.Operations to perform: Apply all migrations: reservationRunning migrations: Applying reservation.0010_auto_20141210_0357...Traceback (most recent call last): File "/Users/chirdeeptomar/envs/mydocbook/lib/python3.4/site-packages/django/apps/registry.py", line 148, in get_app_config return self.app_configs[app_label]KeyError: 'useraccount'
在处理上述异常时,发生另一个异常:
During handling of the above exception, another exception occurred:
推荐答案
以下是一个黑客攻击,在开发或更好的学习之前应用非常困难,您只需要快速解决方案完成这项工作,换句话说,就像苦涩的巧克力一样。
What follows is a hack which is so easy and so nasty to apply while developing or even better while learning other parts of django and you just need a quick solution to get the work done ... in other words, a sin ... like bitter chocolate.
首先,我将数据库数据保存在一个json文件中我需要重建它:
First of all, I keep my database data in a json file in case I need to rebuild it:
./manage.py dumpdata --exclude auth.permission --exclude contenttypes --exclude reversion --exclude admin.LogEntry --indent 2 > db.json
当我更改我的模型中的某些东西,我看到迁移开始抛出错误,我尝试从数据库中删除正在考虑的应用程序的所有内容(或者 - 更好的是整个数据库),然后重建它:
When I change something in my models and I see that migrations start throwing errors, I try deleting everything regarding the application under consideration from the database (or -even better- the whole database) and then I rebuild it:
$./manage.py migrate <myapp1> zero #No need for this if the whole database is destroyed
$rm -Rf <myapp(s/1)>/migrations/*
$./manage.py makemigrations <myapp1>( <myapp2> ... <myappN>)
$./manage.py migrate <myapp(s/1)>
$./manage.py loaddata ...
这是大部分时间更快比调试错误。然而,有时候会导致比应该解决的问题更多的问题。这是当数据库json文件变得方便时。
This is most of the times faster than debugging the error. Sometimes, however, causes more problems than it is supposed to solve. This is when the database json file comes handy.
一个可惜的解决方案,但这是我的报复,当所有这些细节让我很生气,我喜欢它。
A pittiful solution but this is my revenge when all those details make me very angry and I enjoy it.
这篇关于Django 1.7迁移错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!