I am not receiving any error when i run my application but when a user registers he gets added to both client and worker tables. instead of one table
Someone show me where my code is not correct. during registration, am having a user who is a client getting added to both client and worker tables, while a worker is getting added to both client and worker tables, which is not okay. Am not getting any traceback error when i run the app the duplication could mean that there is code which is running twice
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 django.dispatch.dispatcher import receiver
from .models import Worker,Client
@receiver(post_save, sender=User)
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,
email=instance.email,
)
print('Profile created!')
@receiver(post_save, sender=User)
def worker_profile(sender, instance, created, **kwargs):
if created:
Worker.objects.create(
user=instance,
name=instance.username,
email=instance.email,
)
models.py
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
# Create your models here.
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 = (
('Plumber', 'Plumber'),
('Electrician', 'Electrician'),
('Cleaner', 'Cleaner'),
)
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)
bio = models.CharField(max_length=50, 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
views.py
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)
You link both _profile() functions to the save signal so both will create an object