I have two models in Django: User (predefined by Django) and UserProfile. The two are connected through a foreign key.
models.py:
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True, related_name="connect")
location = models.CharField(max_length=20, blank=True, null=True)
I use UserCreationForm (predefined by Django) for the user model and created another form for UserProfile in forms.py
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
exclude = ("user", )
I load both of these forms into the registration.html template, so the site client can enter data about the fields contained in both models (for example: "first_name", "last_name" in the user model, "location" in the UserProfile model).
, . , , User, , UserProfile. - ? , :
def register(request):
if request.method == 'POST':
form1 = UserCreationForm(request.POST)
form2 = UserProfileForm(request.POST)
if form1.is_valid():
username = form1.cleaned_data["username"]
password = form1.cleaned_data["password"]
new_user = User.objects.create_user(username, password)
else:
form1 = UserCreationForm()
form2 = UserProfileForm()
c = {
'form1':UserCreationForm,
'form2':form2,
}
c.update(csrf(request))
return render_to_response("registration/register.html", c)