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

在写博客系统的用户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命令,结果如下



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值