Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Buscar nombre de usuario y apellido Consultar Django

En mi aplicación Django tengo el módulo 'usuarios' que uso para crear perfiles. Necesito ayuda sobre cómo consultar y encontrar el nombre de usuario y el apellido según el perfil en 'usuarios'.

usuarios/modelos.py

 from django.db import models from django.contrib.auth.models import User from PIL import Image from blog.models import Odjel class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) department = models.ForeignKey(Odjel, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="Odjel") image = models.ImageField(default='default.jpg', upload_to='profile_pics') def __str__(self): return f'{self.user.username} Profile' def save(self, *args, **kwargs): super().save(*args, **kwargs) img = Image.open(self.image.path) if img.height > 300 or img.width > 300: output_size = (300, 300) img.thumbnail(output_size) img.save(self.image.path)

No estoy seguro, pero ¿cómo puedo encontrar 'user.first_name' y 'user.last_name' usando solo Profile? Algo como:

 queryset = User.objects.values('first_name').filter(Profile=Profile_var)

vistas.py

 from users.models import Profile from django.http import JsonResponse, HttpResponseRedirect from .models import Post, User, File from users.models import Profile def load_persons(request): #this is Ajax call from my HTML page/script department = request.GET.get('department') print(profile = Profile.objects.filter(department_id=department))

como resultado obtengo <QuerySet [<Profile: JJon Profile>]> Necesito Consultar con JJon para obtener: Nombre: John y Apellido: Jon

Probado hasta ahora:

 username = User.first_name.filter(profile=profile) AttributeError: 'ForwardOneToOneDescriptor' object has no attribute 'first_name' username = Profile.first_name.filter(profile=profile) AttributeError: type object 'Profile' has no attribute 'first_name' username = Profile.user.first_name.filter(profile=profile) AttributeError: 'ForwardOneToOneDescriptor' object has no attribute 'first_name'
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

prueba esto

 Profile.objects.filter(pk=profile_id).values('user__first_name','user__last_name')

ACTUALIZAR para un propósito específico.

 def load_persons(request): #this is Ajax call from my HTML page/script department = request.GET.get('department') profile = Profile.objects.filter(department_id=department) if profile:# we check that the profile exists first first_name = profile[0].user.first_name last_name = profile[0].user.last_name
over 4 years ago · Santiago Trujillo Report

0

Supongamos que tiene una instancia de profile del objeto Profile . Como el objeto User es un OneToOneField para el objeto Profile . Entonces, si tiene una instancia de profile :

 username = profile.user.user_name last_name = profile.user.last_name

Más sobre Django OneToOneField

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!