在my 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将运行两次,我不知道。
我不知道为什么。请帮帮我!

最佳答案

这是not a good way to store additional user information,原因很多,正如James Bennett在链接线程中指出的。这并不奇怪,你得到奇怪的SQL输出,并努力调试它。通过使用相关的配置文件模型来简化工作。

关于python - 有关扩展用户的“User.add_to_class”错误?我不知道为什么,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1389627/

10-11 10:42