Thursday, January 5, 2023

Re: Accessing Data Linked in One-To-One to Current User from a Table


On January 4, 2023 5:11:26 PM CST, G Daniel <> wrote:
Hello All!

I need your help. I have a table, PersonWife, that's linked in a one-to-one fashion with the users (using Django's  user authentication system). See attached models and admin control panel screenshots.

I can get the username of the currently logged in user ( attached screenshots). What I'm trying to do is that having the username of the currently logged in user, I now want to access with whom the user is linked to that one-to-one table and have the name of that person (wife's name).
For example, in my test the user's username is test_user1 and the name of the wife is Carol. How to I get Carol to find Carol in the code?
This is my script so far:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib.sessions.models import Session
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from . models import PersonWife

def home(request):
    context = {}
    return render(request, 'myapp/index.html', context)

def get_wife_name(request):
    #  Check if user is authenticated to get his id
    if request.user.is_authenticated:
        user_username = request.user
        username = user_username.username
        context = {'username':username}
    return render(request, 'myapp/relationship.html', context)


No comments:

Post a Comment