How to make Required: boolean field in model Django

不问归期 提交于 2019-12-06 11:59:16

You can add this fields to StudentResistrationForm and TeacherRegistrationForm and add custom validation for it in clean_fieldname method to make it required:

StudentResistrationForm(ModelForm):
    class Meta:
        model = StudentRegistration
        fields = (
            'instrument',
            'skill',
            'experience_in_years',
            'is_student',
        )

    def clean_is_student(self):
        is_student = self.cleaned_data.get('is_student')
        if not is_student:
            raise forms.ValidationError('This field is required')
        return is_student  

Also in view instead of getting raw data from request.POST you can use forms to create student and teacher objects:

new_user = User.objects.create_user(username=username, email=email, password=password)
teacher = form.save(commit=False)
teacher.user = new_user
teacher.save()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!