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})
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)