M
M
Mitrich30002018-11-25 12:41:31
Django
Mitrich3000, 2018-11-25 12:41:31

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()

I can't add to the code above to set the User.email field to userprofile.contact.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2018-11-25
@planc

importpdb; pdb.set_trace()
inside def new_user() and see what you have in your variables

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question