Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #162 - Changing Username doesn't update the User model username #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions users/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ def create_profile(sender, instance, created, **kwargs):


def update_profile(sender, instance, created, **kwargs):
user_profile, _ = UserProfile.objects.get_or_create(user=instance)
user = instance.user
if created == False:

user_profile.username = instance.username
user.username = instance.username

#instance.userprofile.email = instance.email
user_profile.save()

user.save()
print('Profile updated!')

post_save.connect(create_profile, sender=User)
post_save.connect(update_profile, sender=User)
post_save.connect(update_profile, sender=UserProfile)
7 changes: 7 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMessage
from django.db.models import Q , Count
from django.http import request
from django.template.loader import render_to_string
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
Expand Down Expand Up @@ -239,6 +240,12 @@ class UserProfileUpdate(APIView):


def patch(self, *args, **kwargs):
username = self.request.data.get('username')

# this makes sure that there is no IntegrityError
if User.objects.filter(username=username).exists() and username != self.request.user.username:
return Response( {'success': False, 'message': 'User with that username already exists'},status=400)

profile = self.request.user.userprofile
serializer = self.serializer_class(
profile, data=self.request.data, partial=True)
Expand Down