Well, My Django Administration has the Group customer and staff, but I cant add user with group customer using signal.
models.py
class Customer(models.Model):
user = models.OneToOneField(User, blank= True , null= True, on_delete=models.CASCADE)
name = models.CharField(max_length=255, null=True)
phone = models.CharField(max_length=255, null=True)
email = models.CharField(max_length=255, null=True)
profile_pic = models.ImageField(default="user01.png", null=True, blank=True) # pip install pillow
date_created = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
urls.py
urlpatterns = [
path('register/', views.registerPage, name='register'),
path('login/', views.loginPage, name='login'),
path('logout/', views.logoutUser, name='logout'),
path('', views.home, name='home'),
path('user_page/', views.userPage, name='user_page'),
path('account/', views.accountSettings, name='account'),
path('products/', views.products, name='products'),
path('customer/<str:pk>/', views.customer, name='customer'),
path('create_order/<str:pk>/', views.createOrder, name='create_order'),
path('update_order/<str:pk>/', views.updateOrder, name='update_order'),
path('delete_order/<str:pk>/', views.deleteOrder, name='delete_order'),
]
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, 'accounts/register.html', context)
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 import receiver
from .models import Customer
# create your models here
def customer_profile(sender, instance, created, **kwargs):
if created:
group = Group.objects.get(name='customer')
instance.groups.add(group)
Customer.objects.create(
user=instance,
name=instance.username,
email=instance.email,
)
print('Profile CREATED!')
post_save.connect(customer_profile, sender=User)
settings.py
INSTALLED_APPS = [
ādjango.contrib.adminā,
ādjango.contrib.authā,
ādjango.contrib.contenttypesā,
ādjango.contrib.sessionsā,
ādjango.contrib.messagesā,
ādjango.contrib.staticfilesā,
'accounts.apps.AccountsConfig',
# 'accounts',
'django_filters',
]
apps.py
from django.apps import AppConfig
class AccountsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'accounts'
def ready(self):
import accounts.signals
So, user is created and customer within group costumer not
Seems signal was not called and there isnāt print on terninal from print(āProfile CREATED!ā)