I tried a lot of different tutorials and they all have something missing, repeating unnecessary code or doing strange things, below are my solution, which combines all the parameters that I found, it works, I already entered it in but it's still not convinces me because I expect to get first_name and last_name inside functions that I attached to users to avoid creating a profile inside the form, but I couldnβt, in my opinion, help you.
Models.py
class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) nationality = models.CharField(max_length=2, choices=COUNTRIES) gender = models.CharField(max_length=1, choices=GENDERS) def __str__(self): return self.user.first_name @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
Forms.py
class SignupForm(forms.ModelForm): first_name = forms.CharField(max_length=100) last_name = forms.CharField(max_length=100) class Meta: model = Profile fields = ('first_name', 'last_name', 'nationality', 'gender') def signup(self, request, user):
Settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'apps.profile.forms.SignupForm'
Gregory Apr 08 '17 at 19:58 on 2017-04-08 19:58
source share