唯一约束失败:accounts_user.username [英] UNIQUE constraint failed: accounts_user.username

查看:81
本文介绍了唯一约束失败:accounts_user.username的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们解决这个问题我有一个页面,我希望用户填写有关自己的信息,我想保存该数据.但是我遇到了这个错误.

Let's get to the problem I have a page where I want users to fill info about themselves, and i would like to save that data. But I'm getting this error.

IntegrityError,位于/accounts/profile/edit/1/change-profile/唯一约束失败:accounts_user.username

这是我的观点.py

def change_profile(request, pk):
    form = ChangeProfile(request.POST or None)
    user_ = request.user
    if request.method == "POST":
        if form.is_valid():
            form.save()
            return redirect("/accounts/profile/edit/{}/".format(user_.pk))
    args = {'form': form}
    return render(request, 'change_profile.html', args)

我的模型.py

class GradeUser(models.Model):
    grade = models.CharField(max_length=20, null=True)

class Country(models.Model):
    country = models.CharField(max_length=30, null=True)

class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name = 'email address',
        max_length=255,
        unique=True
        )
    real_name = models.CharField(max_length=20, blank=True)
    username = models.CharField(max_length=40, unique=True, verbose_name='username')
    grade = models.ForeignKey(GradeUser, on_delete=models.CASCADE, blank=True, null=True)
    country = models.ForeignKey(Country, on_delete=models.CASCADE, blank=True, null=True)
    reputation = models.IntegerField(default=0)
    image = models.ImageField(upload_to='accounts/media', blank=True)
    about_me = models.TextField()


    USERNAME_FIELD= 'email'
    REQUIRED_FIELDS = ['username']

    objects = UserManager()

forms.py

class ChangeProfile(forms.ModelForm):
    class Meta:
        model = User
        fields = ('real_name', 'grade', 'country', 'about_me')

究竟出了什么问题?

为什么需要用户名?

要保存该怎么办?

推荐答案

您打算对用户(已经存在)执行更新,但是在这里您要创建一个没有用户名属性的全新用户实例,这就是生成用户名的原因.错误.尝试以下代码:它获取您的当前用户,并使用表单提供的信息对其进行更新.

You aim to perform an update on your User (that already exists) but here you are creating a whole new user instance with no username attribute and that's what generates the error. Try this code : it gets your current user and updates it with the info provided from the form.

PS:我没有对返回值做任何修改,因为我不知道您在那做什么,但是请注意,您不需要通过参数传递用户pk,用户数据已经存储在请求对象中.

PS : I did not modify anything about the returns because I dunno what you're doing there but note that you don't need to pass the user pk through the arguments, the user data is already stored in the request object.

def change_profile(request, pk):
form = ChangeProfile()
user_ = request.user
if request.method == "POST":
    form = ChangeProfile(request.POST)
    if form.is_valid():
        user_new_info=form.save(commit=false)
        user_.real_name = user_new_info.real_name
        user_.grade = user_new_info.grade
        user_.country = user_new_info.country
        user_.about_me = user_new_info.about_me
        user_.save()
        return redirect("/accounts/profile/edit/{}/".format(user_.pk))
args = {'form': form}
return render(request, 'change_profile.html', args)

这篇关于唯一约束失败:accounts_user.username的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆