您的位置:首页 > 编程语言 > Go语言

django.core.exceptions.FieldError: Local field 'id' in class 'User' clashes with field of similar na

2017-08-26 16:43 1176 查看
在写博客系统的用户User类时,我选择通过继承from django.contrib.auth.models import AbstractUser得到的AbstractUser类进行扩展

class User(AbstractUser):
avatar = models.ImageField(upload_to='avatar/%Y/%m', default='avatar/default.png', max_length=200, blank=True, null=True, verbose_name='用户头像')
qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ号码')
mobile = models.CharField(max_length=11, blank=True, null=True, unique=True, verbose_name='手机号码')
url = models.URLField(max_length=100, blank=True, null=True, verbose_name='个人网页地址')

class Meta:
verbose_name = '用户'
verbose_name_plural = verbose_name
ordering = ['-id']

def __str__(self):
return self.username

在执行makemigrations命令时发生错误
Traceback (most recent call last):
File "D:\Python\PyCharm 2016.3.3\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "d:\python\python352\Lib\runpy.py", line 196, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "d:\python\python352\Lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "d:\python\python352\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:/Python/pycharmFiles/blog_project\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\core\management\commands\makemigrations.py", line 132, in handle
migration_name=self.migration_name,
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\autodetector.py", line 45, in changes
changes = self._detect_changes(convert_apps, graph)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\autodetector.py", line 128, in _detect_changes
self.old_apps = self.from_state.concrete_apps
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\state.py", line 166, in concrete_apps
self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\state.py", line 228, in __init__
self.render_multiple(list(models.values()) + self.real_models)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\state.py", line 296, in render_multiple
model.render(self)
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\migrations\state.py", line 585, in render
body,
File "D:\Python\nenv\django_basic_venv\lib\site-packages\django\db\models\base.py", line 223, in __new__
'base class %r' % (field.name, name, base.__name__)
django.core.exceptions.FieldError: Local field 'id' in class 'User' clashes with field of similar name from base class 'User'django提示本地字段的id与AbstractUser的id冲突了。后来发现这是由于之前执行过makemigrations命令导致在应用下的migrations文件夹存在残留的文件引起的。在把migrations文件夹中的除__init__().py以外的文件删除后能正常执行makemigrations命令,结果如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐