0

When I am using user.email_confirmed I am not getting any error but when using user.userprofile.email_confirmed getting RelatedManager error. Why I am getting this error when trying to get user details from userprofile model?

here is my code:

class UserManagement(AbstractUser):
          is_blog_author = models.BooleanField(default=False)
          is_editor = models.BooleanField(default=False)
          is_subscriber = models.BooleanField(default=False)
          is_customer = models.BooleanField(default=False)
          email_confirmed = models.BooleanField(default=False) #there is no error when obtaining email_confirmed object from abstract user model.  

class UserProfile(models.Model):
      user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,related_name="userprofile")
      email_confirmed = models.BooleanField(default=False)


#I am using this for email verification 
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            six.text_type(user.pk) + six.text_type(timestamp) +
            six.text_type(user.userprofile.email_confirmed) #error rising for this line 
            #six.text_type(user.email_confirmed) #I am not getting error if I use this line 
        )

account_activation_token = AccountActivationTokenGenerator()

views.py

....others code 
if user is not None and account_activation_token.check_token(user, token):
            user.userprofile.email_confirmed = True #why it's not working and rising error? 
            #user.email_confirmed = True #it's working 
            user.save()
            .....others code 
boyenec
  • 1,405
  • 5
  • 29
  • 1
    UserProfile uses a ForeignKey, rather than a OneToOneField, so it is possible for each user to have multiple UserProfile objects. You therefore need to filter the `user.userprofile` queryset down to a single object before you can use it. See also: https://stackoverflow.com/questions/5870537/whats-the-difference-between-django-onetoonefield-and-foreignkey – Nick ODell Oct 04 '21 at 02:11
  • @Nick ODell should I use user.userprofile.filter(email_confirmed = True) for six??? can you please show me which query set will be appropriate for `user.userprofile.email_confirmed` and `user.userprofile.email_confirmed = True` – boyenec Oct 04 '21 at 02:13

1 Answers1

0

as @Nick ODell told me I need to be use filter.

The problem was occurring for this two queryset: user.userprofile.email_confirmed and user.userprofile.email_confirmed = True after I changed theme

user.userprofile.filter(email_confirmed=False) and user.userprofile.filter(email_confirmed=False).update(email_confirmed=True) my problem solved.

boyenec
  • 1,405
  • 5
  • 29