使用CustomUser作为作者给数据库带来了问题?

时间:2018-09-18 13:46:23

标签: django

我有点困惑,我在设置(AUTH_USER_MODEL = 'users.CustomUser')中使用CustomUser,并且想在评论中使用用户名作为作者。

class Comment(models.Model):
    post = models.ForeignKey('blog.Post', on_delete=models.CASCADE, related_name='comments')
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True)
    text = models.TextField()

当我运行迁移时,它给了我这个错误

  

返回self.cursor.execute(sql,params)django.db.utils.DataError:   整数的无效输入语法:“ Alandivar”

Alandivar是我在devmode中的超级用户名,

因此,当客户发表评论(需要登录)时,用户名将自动用作作者

致谢

customuser表单

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from django import forms

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = CustomUser
        fields = ('username', 'email')

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = UserChangeForm.Meta.fields

customuser模型

from django.contrib.auth.models import AbstractUser, UserManager
from django.db import models

class CustomUserManager(UserManager):
    pass

class CustomUser(AbstractUser):
    objects = CustomUserManager()

customuser视图

from django.urls import reverse_lazy
from django.views import generic

from .forms import CustomUserCreationForm

class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

0 个答案:

没有答案