Django - Create user profile on user creation Django - Create user profile on user creation django django

Django - Create user profile on user creation


You shouldn't use:

user = models.ForeignKey(User, unique=True)

Instead use this:

from django.conf import settings..user = models.OneToOneField(settings.AUTH_USER_MODEL)


Just figured it out.

I forgot to add null=True to the rest of UserProfile model fields.

So the accounts.models.UserProfile fields now looks like:

user = models.ForeignKey(User, unique=True)birth_date = models.DateField(null=True)genre = models.CharField(max_length=1, choices=GENRE_CHOICES, null=True)address = models.CharField(max_length=150, null=True)postal_code_4 = models.PositiveIntegerField(null=True)postal_code_3 = models.PositiveIntegerField(null=True)locatity = models.CharField(max_length=30, null=True)marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES, null=True)child_amount = models.PositiveSmallIntegerField(null=True)is_merchant = models.BooleanField(default=False)store = models.ForeignKey(Store, null=True)

...and everything is working as intended!

Cheers for trying to help Ashray ^^


You can create this using Django Signals. Reference https://docs.djangoproject.com/en/2.2/topics/signals/

Add this to installed_apps in settings.py

# settings.pyINSTALLED_APPS = [    ...    'accounts.apps.AccountsConfig',    ...]

Note: if you use accounts in INSTALLED_APPS, you'll have to initialise in init.py

In models.py remove create_user_profile

# models.pyfrom django.db import modelsfrom django.db.models.signals import post_savefrom django.contrib.auth.models import Userfrom main.models import Storeclass UserProfile(models.Model):    GENRE_CHOICES = (        ('m', 'Masculino'),        ('f', 'Feminino'),    )    MARITAL_STATUS_CHOICES = (        ('s', 'Solteiro'),        ('c', 'Casado'),        ('d', 'Divorciado'),        ('v', 'Viรบvo'),    )    user = models.OneToOneField(User)    birth_date = models.DateField()    genre = models.CharField(max_length=1, choices=GENRE_CHOICES)    address = models.CharField(max_length=150)    postal_code_4 = models.PositiveIntegerField()    postal_code_3 = models.PositiveIntegerField()    locatity = models.CharField(max_length=30)    marital_status = models.CharField(max_length=1, choices=MARITAL_STATUS_CHOICES)    child_amount = models.PositiveSmallIntegerField()    is_merchant = models.BooleanField(default=False)    store = models.ForeignKey(Store, null=True)

Add create_user_profile in signals.py

# signals.pyfrom django.contrib.auth.models import Userfrom django.db.models.signals import post_savefrom django.dispatch import receiverfrom .models import UserProfile@receiver(post_save, sender=User)def create_user_profile(sender, instance, created, **kwargs):    if created:        UserProfile.objects.create(user=instance)@receiver(post_save, sender=User)def save_user_profile(sender, instance, **kwargs):    instance.profile.save()

Finally, edit apps.py

# apps.pyfrom django.apps import AppConfigclass AccountsConfig(AppConfig):    name = 'accounts'    def ready(self):        import accounts.signals