[Answered ]-Django, register extended user model

2👍

You want to create a Teacher model but your modelform’s model is User – so you’re not saving what you want in the way you want.

The absolute easiest, but not necessarily the most elegant, way to do this is with two separate forms.

class UserRegistrationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'password1', 'password2')


class RegistrationFormTeacher(forms.ModelForm):
    class Meta:
        model = Teacher
        fields = (modules, confirmed)

Then when you’re processing your RegistrationFormTeacher:

if request.method == 'POST':
    form = RegistrationFormTeacher(request.POST)
    if form.is_valid():
        new_teacher = form.save(commit=False)
        new_teacher.user = request.user #get the user object however you want - you 
            #can pass the user ID to the view as a parameter and do 
            #User.objects.get(pk=id) or some such, too. 
        new_teacher.save()
        form.save_m2m()

Leave a comment