https://docs.djangoproject.com/en/4.1/topics/db/queries/#one-to-one-relationships
On January 4, 2023 5:11:26 PM CST, G Daniel <bgeorgesd@gmail.com> 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_404from django.http import HttpResponseRedirectfrom django.contrib.sessions.models import Sessionfrom django.contrib.auth.models import Userfrom django.contrib.auth.decorators import login_requiredfrom . models import PersonWifedef home(request):context = {}return render(request, 'myapp/index.html', context)@login_requireddef get_wife_name(request):# Check if user is authenticated to get his idif request.user.is_authenticated:user_username = request.userusername = user_username.usernameprint(username)context = {'username':username}return render(request, 'myapp/relationship.html', context)Thanks
No comments:
Post a Comment