SUDAPP SUDAPP - 7 months ago 115
Python Question

confirmation Email and how to access and alter user's profile fields?

I am following a tutorial and everything is working fine, except being able to access the user's profile fields , and so far i am able to set the 'is_active' flag on User model, put 'user.profile.email_confirmed' is really not changing at all,so any Idea about what is that i am doing wrong.

models.py



class Profile(models.Model):
# relations
user = models.OneToOneField(User, on_delete=models.CASCADE)
email_confirmed = models.BooleanField(default=False)


tokens.py



class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.profile.email_confirmed)
)


account_activation_token = AccountActivationTokenGenerator()


urls.py



url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]
{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate, name='activate'),


views.py



def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None

if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.profile.email_confirmed = True
user.save()
login(request, user)
return redirect('home')
else:
return render(request, 'account_activation_invalid.html')

Answer Source

Try inserting

user.profile.save()

before your

user.save()

call.

It may help to view things from the underlying DBs perspective. Usually relations are stored as integers representing foreign keys to an entry in another database table.

Thus when you execute

user.save()

you are writing to the DB that is_active is now set to True but the user profile is still the same integer value as before, as you didn't change the profile object that the user has. As far as I know, Django doesn't go out of its way to figure out that the profile object has also been changed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download