【问题标题】:AttributeError: 'auth.User' has been swapped for 'accounts.Accounts'? How?AttributeError:'auth.User' 已被交换为'accounts.Accounts'?如何?
【发布时间】:2018-09-12 04:29:11
【问题描述】:
  • 所以我有一个扩展AbstractUser 模型的Accounts 模型。

  • 我也有 StudentProfileTeacherApplications 有一个 与Accounts 的一对一关系。

  • 我还有两张表格供老师和学生填写。

要求:让学生/教师能够通过他们的表格进行注册。

问题:在学生/教师表单中,我要求用户模型需要的字段,例如 email, username, first and last name etc.. 但是我得到了这个:。

错误:1

/register/ 管理器中的 AttributeError 不可用; 'auth.User' 有 已换成“accounts.Accounts”

错误 2

Student_profile.save()
                     ^ TabError: inconsistent use of tabs and spaces in indentation

问题:我需要使用用户字段注册用户,否则用户字段将为空并生成错误。但是最好的方法是什么?还是我做错了什么?

型号

class Accounts(AbstractUser):
    email = models.EmailField('email address', unique=True)
    first_name = models.CharField('first name', max_length=30, blank=True)
    last_name = models.CharField('last name', max_length=30, blank=True)
    date_joined = models.DateTimeField('date joined', auto_now_add=True)

    # asdd
    bio = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)

class StudentProfile(models.Model):
    user = models.OneToOneField('Accounts', related_name='student_profile')
    # additional fields for students
    AMEB_Ratings = models.PositiveIntegerField(default=0)
    is_student = models.BooleanField('student status', default=False)



class TeacherApplications(models.Model):
    user = models.OneToOneField('Accounts', related_name='teacher_profile')
    # additional fields for teachers
    instrument = models.TextField(max_length=500, blank=True)
    skill = models.CharField(max_length=30, blank=True)
    experience_in_years = models.PositiveIntegerField(blank=True)
    is_teacher = models.BooleanField('teacher status', default=False)

表格

class StudentResistrationForm(forms.ModelForm):
    class Meta:
        model = StudentProfile
        fields = (  
            # 'username',
            'first_name',
            'last_name',
            'email',
            'date_joined',
#           'password1',
#           'password2',
            'AMEB_Ratings',
            'is_student',

        )

    def save(self, commit=True):
        user = super(StudentResistrationForm, self).save(commit=False)
        # user.first_name = self.cleaned_data['first_name']
        # user.last_name = self.cleaned_data['last_name']
        user.AMEB_Ratings = self.cleaned_data['AMEB_Ratings']

        if commit:
            user.save()

        return user


class TeacherRegistrationForm(forms.ModelForm):
    class Meta:
        model = TeacherApplications
        fields = (
            'instrument',
            'skill',
            'experience_in_years',
            'is_teacher',
        )

观看次数

def registerStudent(request):
    # Once register page loads, either it will send to the server POST data (if the form is submitted), else if it don't send post data create a user form to register
    if request.method == "POST":
        user_form = UserForm(request.POST)
        form = StudentResistrationForm(request.POST)

        if form.is_valid() and user_form.is_valid():
            User = get_user_model()
            username = user_form.cleaned_data['username']
            email = user_form.cleaned_data['email']
            email = user_form.cleaned_data['password']
            new_user = User.objects.create_user(username=username, email=email, password=password)

            Student_profile = StudentProfile()
            Student_profile.user = new_user
            Student_profile.AMEB_Ratings = request.POST['AMEB_Ratings']
            # Student_profile = StudentProfile.create_user(AMEB_Ratings=AMEB_Ratings)
            new_user.save()
            Student_profile.save()
            # form.save()

            # AMEB_Ratings = form.cleaned_data['AMEB_Ratings']
            return redirect('../home/')
    else:
        #  Create the django default user form and send it as a dictionary in args to the reg_form.html page.
        user_form = UserForm()
        form = StudentResistrationForm()        

        args = {'form_student': form, 'user_form': user_form }
    return render(request, 'accounts/reg_form_students.html', args)

def teacherApplication(request):
    # # Once register page loads, either it will send to the server POST data (if the form is submitted), else if it don't send post data create a user form to register
    # if request.method == "POST":
    #   form = TeacherRegistrationForm(request.POST)
    #   if form.is_valid():
    #       instrument = form.cleaned_data['instrument']
    #       skill = form.cleaned_data['skill']
    #       experience_in_years = form.cleaned_data['experience_in_years']
    #       is_teacher = form.cleaned_data['is_teacher']
    #       form.save()
    #       return redirect('../home/')
    # else:
    #   #  Create the django default user form and send it as a dictionary in args to the reg_form.html page.
    #   user_form = UserForm()
    #   form = StudentResistrationForm()        
 #  return render(request, 'accounts/reg_form_teachers.html', {'form_student': form, 'user_form': user_form })
    pass

【问题讨论】:

  • 您的第一个错误是从哪里得到的?你能和我们分享你的堆栈跟踪吗?

标签: python django authentication model registration


【解决方案1】:

我认为你应该把你的模型改成这样,我的意思是StudentTeacher应该从帐户继承,所以你不再需要is_teacheris_student了。

class StudentProfile(Accounts):
    AMEB_Ratings = models.PositiveIntegerField(default=0)



class TeacherApplications(Accounts):
    instrument = models.TextField(max_length=500, blank=True)
    skill = models.CharField(max_length=30, blank=True)
    experience_in_years = models.PositiveIntegerField(blank=True)

对于您的第二个错误,我不得不说问题是缩进,您应该将 tabs 替换为 4 个空格。 我还需要你的urls.py 文件来完全弄清楚它,我认为可能存在问题。 这个链接可能有用 Manager isn't available; User has been swapped for 'pet.Person'

【讨论】:

    猜你喜欢
    • 2018-03-19
    • 2020-09-20
    • 1970-01-01
    • 2021-12-24
    • 2019-07-20
    • 2018-07-27
    • 2020-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多