Answer the question
In order to leave comments, you need to log in
Django 2 - how to assign a value to the User.email field when creating a UserProfile?
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User, on_delete=models.CASCADE)
# The additional attributes we wish to include.
born = models.CharField("Год рождения", max_length=4)
contact = models.EmailField("Контакты", unique=True, error_messages={
'unique': "Пользователь с таким адресом уже существует.",
}, )
@receiver(post_save, sender=User)
def new_user(sender, instance, created, **kwargs):
<b> instance.email = instance.userprofile.contact</b>
if created:
UserProfile.objects.create(user=instance)
instance.userprofile.save()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question