【问题标题】:AN error about "User.add_to_class" to extend my user?I do not know why关于“User.add_to_class”扩展我的用户的错误?我不知道为什么
【发布时间】:2009-09-07 14:17:05
【问题描述】:

在我的 models.py 中,我使用这些代码来扩展两个字段:

User.add_to_class('bio', models.TextField(blank=True))
User.add_to_class('about', models.TextField(blank=True))

但是当我创建一个用户时:

user = User.objects.create_user(username=self.cleaned_data['username'], \
            email=self.cleaned_data['email'],password=self.cleaned_data['password1'])

有这样的错误:

ProgrammingError at /account/register/
(1110, "Column 'about' specified twice")

Request Method: POST 
Request URL: http://127.0.0.1:8000/account/register/ 
Exception Type: ProgrammingError 
Exception Value: (1110, "Column 'about' specified twice") 

我检查了 django 创建的 sql,我发现它很奇怪:

'INSERT INTO `auth_user` (`username`, `first_name`, `last_name`, `email`, `password`, `is_staff`, `is_active`, `is_superuser`, `last_login`, `date_joined`, `about`,'bio','about','bio') VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'

有两个 about 和 bio 。但是在 Mysql 表中只有一个 'about' 和 'bio'。 另一方面,在这种情况下,models.py 将运行两次,我不知道。

我不知道为什么。请帮助我!

【问题讨论】:

  • models.py 是否被执行了两次?你能保护User.add_to_class代码吗?

标签: python mysql django


【解决方案1】:

这是not a good way to store additional user information,原因有很多,正如 James Bennett 在链接线程中指出的那样。毫无疑问,您会得到奇怪的 SQL 输出并努力调试它。改用相关的配置文件模型,让事情变得简单。

【讨论】:

  • 有趣,感谢您提供指向 googlegroup 的链接。我记得当我想从用户继承时,我遇到了一些不兼容的问题,所以我停止了运行良好的配置文件方法。
猜你喜欢
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多