Django:ValueError:查找字段account.UserProfile.user引用的模型失败:auth.User
在运行 python manage.py migrate
时出现了这个错误:
ValueError: 查找模型失败,字段 account.UserProfile.user 参考的模型是: auth.User
我做了以下步骤:
1. 创建了项目并添加了新的应用:
$ django-admin.py startproject djdev
$ cd djdev
$ python manage.py startapp account
2. 我把新应用添加到了 INSTALLED_APPS
中,位置在 djdev/settings.py
:
...
'django.contrib.staticfiles',
'account',
)
...
3. 在 account/models.py
中创建了一个新的 UserProfile
模型类:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
"""
User Profile having one-to-one relations with User
"""
class Meta:
db_table = 'user_profile'
ordering = ['id']
user = models.OneToOneField(User, db_column='id_user', related_name='profile')
mobile_no = models.CharField('Mobile no.', db_column='contact_no_home', max_length=16, blank=True, null=True)
address_line_1 = models.CharField('Address Line 1', db_column='contact_address_line_1_home', max_length=140, blank=True, null=True)
address_line_2 = models.CharField('Address Line 2', db_column='contact_address_line_2_home', max_length=140, blank=True, null=True)
office_mobile_no = models.CharField('Mobile no.', db_column='contact_no_office', max_length=16, blank=True, null=True)
office_address_line_1 = models.CharField('Address Line 1', db_column='contact_address_line_1_office', max_length=140, blank=True, null=True)
office_address_line_2 = models.CharField('Address Line 2', db_column='contact_address_line_2_office', max_length=140, blank=True, null=True)
about = models.TextField('About me', blank=True, null=True)
note = models.CharField('Note', max_length=255, blank=True, null=True)
def __unicode__(self):
return self.user.name
4. 开始迁移:
$ python manage.py makemigrations account
$ python manage.py migrate
在执行最后一个命令 python manage.py migrate
后,我收到了这个错误:
Operations to perform:
Synchronize unmigrated apps: (none)
Apply all migrations: admin, contenttypes, account, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying account.0001_initial...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/vinay/python_webapps/django-trunk/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/home/vinay/python_webapps/django-trunk/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/vinay/python_webapps/django-trunk/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/vinay/python_webapps/django-trunk/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "/home/vinay/python_webapps/django-trunk/django/core/management/commands/migrate.py", line 146, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/vinay/python_webapps/django-trunk/django/db/migrations/executor.py", line 62, in migrate
self.apply_migration(migration, fake=fake)
File "/home/vinay/python_webapps/django-trunk/django/db/migrations/executor.py", line 90, in apply_migration
if self.detect_soft_applied(migration):
File "/home/vinay/python_webapps/django-trunk/django/db/migrations/executor.py", line 134, in detect_soft_applied
apps = project_state.render()
File "/home/vinay/python_webapps/django-trunk/django/db/migrations/state.py", line 83, in render
model=lookup_model
ValueError: Lookup failed for model referenced by field account.UserProfile.user: auth.User
注意:我使用的 Django 版本是:1.8.dev20140507130401
3 个回答
2
我找到了一种其他的解决办法,也很好用。
可以看看rockallite.wulf@的解决方案,链接在这里:https://code.djangoproject.com/ticket/22488
只需要在迁移文件中添加相应的依赖,就像这样:
dependencies = [ (b'advperm', b'0001_initial'), (b'auth', b'__first__'), (b'contenttypes', b'__first__'), # Add this line ]
3
我看到上面的评论是建议只在依赖列表中添加最后一行 (('contenttypes',' __first__'))
,但我发现我需要添加最后两行才能解决这个问题。对于那些对Django中的 makemigrations
工具了解得更好的人来说,这可能很明显,但我对它还很陌生,所以对我来说并不是这样。
我之前的迁移文件依赖列表,原本也出现了同样的错误……
dependencies = [
('myapp', '0006_auto_20150209_0324'),
(b'auth', b'__first__'),
(b'contenttypes', b'__first__'),
]
6
这个问题已经在主分支上修复了。
修复的提交记录:
- https://code.djangoproject.com/changeset/8f6dff372b174e772920de6d82bd085f1a74eaf2
- https://code.djangoproject.com/changeset/35c2a14a49ac3cb25dcff818b280bf0b4c290287
你可以在正式版本发布之前先安装这个版本:
pip install https://github.com/django/django/zipball/master
测试:
models.py
from django.db import models
from django.contrib.auth.models import User
class Test(models.Model):
user = models.OneToOneField(User)
结果
[__env] $ ./manage.py makemigrations
Migrations for 'data':
0001_initial.py:
- Create model Test
[__env] $ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: admin, contenttypes, auth, sessions
(... ommited ...)
Running migrations:
Applying data.0001_initial... OK