I have two groups, 'client' and 'worker'.. when i register a client everything is fine but for a worker he gets assigned to both groups
i have two groups, 'client' and 'worker'.. when i register a client everything is fine but for a worker he gets assigned to both groups. i need everyone to be assigned to their specific group
Signals.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from .models import Worker,Client
def client_profile(sender, instance, created, **kwargs):
if created:
group = Group.objects.get(name='client')
instance.groups.add(group)
Client.objects.create(
user=instance,
name=instance.username,
)
print('Profile created!')
post_save.connect(client_profile, sender=User)
def worker_profile(sender, instance, created, **kwargs):
if created:
group = Group.objects.get(name='worker')
instance.groups.add(group)
Worker.objects.create(
user=instance,
name=instance.username,
)
print('profile created!')
i have two groups, 'client' and 'worker'.. when i register a client everything is fine but for a worker he gets assigned to both groups. i need everyone to be assigned to their specific group
views.py
@unauthenticated_user
def registerPage(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(request, 'Account was created for ' + username)
return redirect('login')
context = {'form': form}
return render(request, 'users/register.html', context)
def workerRegister(request):
if request.user.is_authenticated:
return redirect('plumber_home')
else:
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
user = form.save()
username = form.cleaned_data.get('username')
messages.success(
request, 'Account was created for ' + username)
return redirect('worker_login')
context = {'form': form, }
return render(request, 'users/worker_register.html', context)
i have two groups, 'client' and 'worker'.. when i register a client everything is fine but for a worker he gets assigned to both groups. i need everyone to be assigned to their specific group
models.py
class Client(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
profile_pic = models.ImageField(default="profile1.png", null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
class Worker(models.Model):
CATEGORY = (
('Plumbing', 'Plumbing'),
('Electrical', 'Electrical'),
('Cleaning', 'Cleaning'),
)
user = models.OneToOneField(
User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
profile_pic = models.ImageField(
default="profile2.png", null=True, blank=True)
area_of_operation = models.CharField(max_length=200, null=True)
category = models.CharField(max_length=200, null=True, choices=CATEGORY)
description = models.CharField(max_length=200, null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True, null=True)
date_updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
you post_save the client_profile when user is created, this also means when a worker is created the user is post_saved as client profile. Use signals for both and put an @receiver above the function.