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