Answer the question
In order to leave comments, you need to log in
How to display a registration form with additional fields?
There is a model that extends the standard User model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
producer_name = models.CharField(max_length=100, verbose_name="Producer name", blank=True)
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
def register2(request):
FormSet = inlineformset_factory(User, Profile, fields=('producer_name', 'username'))
if request.method == "POST":
formset = FormSet(request.POST)
if formset.is_valid():
pass
else:
formset = inlineformset_factory(User, Profile, fields=('producer_name', 'username'))
return render(request, 'blog/register-second.html', {'formset': formset})
Answer the question
In order to leave comments, you need to log in
Two forms:
userForm
formProfile
userForm.as_p
formProfile.as_p
And when accepting forms, save them one by one:
userForm.is_valid()
formProfile.is_valid()
PS I did it through an abstract model .
My example,
models.py should help:
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
is_client = models.BooleanField("Статус клиента", default=False)
class Client(models.Model):
"""Профиль клиентов"""
user = models.OneToOneField(User,
on_delete=models.CASCADE,
primary_key=True)
entity = models.CharField("Организация", max_length=255, blank=True)
address = models.TextField("Адрес организации", blank=True)
mode = models.CharField("Режим работы", max_length=255, blank=True)
created = models.DateTimeField(auto_now_add=True)
class Meta():
verbose_name = "Профиль клиента"
verbose_name_plural = "Профили клиентов"
ordering = ["-created"]
def __str__(self):
return self.name
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from main.models import Client, User
class ClientCreationForm(UserCreationForm):
entity = forms.CharField(required=False, label='Организация')
address = forms.CharField(required=False, widget=forms.Textarea, label='Адрес')
mode = forms.CharField(required=False, label='График работы')
class Meta(UserCreationForm.Meta):
model = User
@transaction.atomic
def save(self):
user = super().save(commit=False)
user.is_client = True
user.save()
Client.objects.create(user=user,
entity=self.cleaned_data.get('entity'),
address=self.cleaned_data.get('address'),
mode=self.cleaned_data.get('mode'))
return user
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm
from .models import User
class MyUserChangeForm(UserChangeForm):
class Meta(UserChangeForm.Meta):
model = User
class MyUserAdmin(UserAdmin):
form = MyUserChangeForm
fieldsets = UserAdmin.fieldsets + (
('Статус', {'fields': ('is_client', )}),
)
admin.site.register(User, MyUserAdmin)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question