Хорошо, на основании вашего комментария в ответ на @J.Lnadgrave, давайте предположим, что у вас есть свойство «user_type» в вашей модели UserProfile, которое можно установить для вашего «обычного» пользователя или «корпоративного» пользователя ...
#your_app.constants
NORMAL_USER = 0
COMPANY_USER = 1
USER_TYPE_CHOICES = (
(NORMAL_USER, "Normal"),
(COMPANY_USER, "Company"),
)
#your_app.models
from django.contrib.auth.models import User
from your_app.constants import USER_TYPE_CHOICES
class UserProfile(models.Model):
user = models.OneToOne(User)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
#your_app.forms
from your_app.models import UserProfile
class UserProfileForm(forms.ModelForm):
class Meta():
model = UserProfile
user_type = forms.IntegerField(widget=forms.HiddenInput)
#your_app.views
form django.http import HttpResponseRedirect
from django.shortcuts import render
from your_app.constants import NORMAL_USER, COMPANY_USER
from your_app.forms import UserProfileForm
from your_app.models import UserProfile
def normal_user_registration(request):
user_profile_form = UserProfileForm(request.POST or None,
initial={'user_type' : NORMAL_USER})
if request.method == 'POST':
user_profile_form.save()
return HttpResponseRedirect('/')
return render(request, 'registration/registration.html',
{'user_profile_form' : user_profile_form})
def company_user_registration(request):
user_profile_form = UserProfileForm(request.POST or None,
initial={'user_type' : COMPANY_USER})
if request.method == 'POST':
user_profile_form.save()
return HttpResponseRedirect('/')
return render(request, 'registration/registration.html',
{'user_profile_form' : user_profile_form})
Это довольно долгий путьподойти к этому, но я подумал, что это довольно очевидно, как передать это начальное значение в вашу форму.Надеюсь, это поможет вам.