Home > Back-end >  How Can I check if logged in user profile information is updated in django models
How Can I check if logged in user profile information is updated in django models

Time:05-03

I am working on a project in Django where I have a Profile Model with a Foreign Key field (OneToOne Relationship with User Model) called applicant and a status field which is set to 'Update' at default. Other fields like surname and othernames are also included in the Profile Model.

class Profile(models.Model):
applicant = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
surname = models.CharField(max_length=10, null=True)
othernames = models.CharField(max_length=30, null=True)
status = models.CharField(max_length=30, default='Update', null=True)

def save(self, *args, **kwargs):
    self.profilestatus = 'Updated'
    super().save(*args, **kwargs)

def __str__(self):
    return f'{self.applicant.username}-Profile'

I want the system to check every logged in user if their profile information are not Updated, they should be redirected to the Profile Update page to update their profile first else they should be redirected to view their updated profile.

def index(request):

user = request.user.is_authenticated

if request.user.is_authenticated and Profile.objects.get(applicant=user, status= 'Update'):
    
    
    
    return redirect('user-profile-update')
else:
    return redirect('user-profile')

context = {
    'check_profile_update':check_profile_update,
}
return render(request, 'dashboard/index.html', context)

I have ModelForm with a save method with automatically updates the satus field in Profile Model to Updated any time a user updates his or her profile information.

The issue is anytime I run the app error says 'Profile matching query does not exist.'

Someone should kindly help me on the best way of solving this issue. In fact, how can I create an instance of the logged in user so I can check the user profile update status in model? Thanks

CodePudding user response:

The Problem:
user = request.user.is_authenticated makes the variable user = True, so when you use it in
if request.user.is_authenticated and Profile.objects.get(applicant=user, status= 'Update'):,
you are really doing the following:
if request.user.is_authenticated and Profile.objects.get(applicant=True, status= 'Update'):
So of course there will be no matches since no applicant equals True.

The Solution
Change the two lines to:

user = request.user    # Now user equals the actual user
if user.is_authenticated and Profile.objects.get(applicant=user, status= 'Update'):

On another note:
I hesitate to use a get query without putting it in a try/except statement so that you can handle what should happen if a Profile is not found:

user = request.user    # Now user equals the actual user
if user.is_authenticated:
    try:
        profile = Profile.objects.get(applicant=user, status= 'Update')
    except Profile.DoesNotExist:
        # Code to handle what happens if a Profile is NOT found
        # Maybe redirect to a page not found
        # or maybe redirect to a page to create an applicant?      

EDIT

def index(request):
    user = request.user
    if user.is_authenticated:
        try:
            profile = Profile.objects.get(applicant=user)
        except Profile.DoesNotExist:
            print("Profile Does Not Exist")
            # Redirect to a Profile creating page?
        else:
            if profile.status == 'Update':
                return redirect('user-profile-update')
            else:
                return redirect('user-profile')
    
context = {
    'check_profile_update':check_profile_update,
}
return render(request, 'dashboard/index.html', context)

Edit 2
As per your comment, the final working version is then:

def index(request):
    user = request.user
    if user.is_authenticated:
        try:
            profile = Profile.objects.get(applicant=user)
        except Profile.DoesNotExist:
            print("Profile Does Not Exist")
            # Redirect to a Profile creating page?
        else:
            if profile.surname == None:
                return redirect('user-profile-update')
            else:
                return redirect('user-profile')
    
context = {
    'check_profile_update':check_profile_update,
}
return render(request, 'dashboard/index.html', context)
  • Related