简体   繁体   中英

I don't know how to display my customuser and user in same form

I have 2 models: teacher and student. They both extend User class with a OneToOneField and they both have receivers defined for creation and saving. Now, in my forms I can only display the fields from student or teacher only, the other fields that come with user I don't know how to include them. But I want that a student or a teacher won't be able to create account, unless all fields are filled in. Here are my forms and view:

 class StudentSignUpForm(forms.ModelForm): class Meta: model = Student fields = ('student_ID', 'photo', 'phone') class TeacherSignUpForm(forms.ModelForm): class Meta: model = Teacher fields = ('academic_title', 'photo', 'phone', 'website', 'bio') 

 class StudentSignUpView(CreateView): model = User form_class = StudentSignUpForm template_name = 'student_signup_form.html' def get_context_data(self, **kwargs): kwargs['user_type'] = 'student' return super().get_context_data(**kwargs) def form_valid(self, form): user = form.save() login(self.request, user) return redirect('index') class TeacherSignUpView(CreateView): model = User form_class = TeacherSignUpForm template_name = 'teacher_signup_form.html' def get_context_data(self, **kwargs): kwargs['user_type'] = 'teacher' return super().get_context_data(**kwargs) def form_valid(self, form): user = form.save() login(self.request, user) return render_to_response('index.html', {'form': form},) 

You can try creating another form for the User model and just include UserForm and StudentSignupForm in one <form></form> tag, and another UserForm and TeacherSignUpForm in another <form> tag. You can then handle the data separately in your view.

In this case, you will have to use a custom View instead of CreateView because CreateView typically only allows for one form_class .

You can do something like

class TeacherSignupView(View):
    def get_context_data(self):
        return {'user_form': self.user_form_class, 'teacher_form': self.teacher_form_class}

    def get(self, request, *args, **kwargs):
        return render(request, 'form.html', self.get_context_data())

    def post(self, request, *args, **kwargs):
        user_data = {
           'some-user-field': request.POST.get('some-user-field')
        }
        teacher_data = {
           'some-teacher-field': request.POST.get('some-teacher-field')
        }

        # Handle each set of form data individually
        user_form = self.user_form_class(user_data)

        # Check valid
        if user_form.is_valid():
            user = user_form.save()
            # If user form is valid and user data is saved, then proceed with teacher data
            teacher_data["user"] = user  # add the user relation here
            teacher_form = self.teacher_form_class(teacher_data)
            if teacher_form.is_valid():
                teacher_form.save()

        return render_to_response('index.html', self.get_context_data())

But I want that a student or a teacher won't be able to create account, unless all fields are filled in.

They're starting to sound like separate models.

If they share some commonality, or you want to use them in the same place sometimes, I would recommend looking into django-polymorphic . It sounds scary, but it's just a way to have an inheritance tree for models, and use the parent class when you can, as if it was a normal model on its own. It has fantastic admin and form integration, and feels like part of django itself.

In your instance, you'll be able to have your models defined as such:

class BaseUser(PolymorphicModel):
    photo = models.FileField(...)
    phone = models.CharField(...)
    ...


class Student(BaseUser):
    student_ID = models.CharField(...)
    ...

class Teacher(BaseUser):
    academic_title = models.CharField(...)
    website = models.URLField(...)
    bio = models.TextField(...)
    ...

And the database, along with polymorphic and your bog-standard ModelForm s, will ensure completeness for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM