Retrieve the value of a field linked by foreign key to a Profile that extends the User (oneToOne relationship) in Django views.py

What I want to do :

Display a phone book from Django User model extended with a Profile model related to several models

What I have done :

Of course, I've read Django documentation (4.1)

  • I have my classic Django User model

  • I have created a "Profile" model to extend the User model via a OneToOne relationship (here simplified) :

class Profile(models.Model):
        
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    entity = models.ForeignKey(Entity, null=True, blank=True, on_delete=models.CASCADE) 


    class Meta:
        ordering = ['entity__name', 'user__last_name']

    def __str__(self):
        return self.user.first_name + " " + self.user.last_name
        
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
  • I have created an "Entity" model (more or less the company where people work) (here simplified) :
class Entity(CommonFieldsUUID):

    name = models.CharField(max_length=100, unique=True, null=False, default="N.C.")
    alias = models.CharField(max_length=25, unique=True, null=False, default="N.C.")
    
    class Meta:
        verbose_name = "Entity"
        verbose_name_plural = "Entities"

    def __str__(self):
        return self.alias
  • Here is my views.py :
from .models import User
from django.http import HttpResponse
from django.template import loader


def phonebook(request):
    user = User.objects.filter(is_active=True).values('first_name','last_name','email','profile__entity','profile__pro_ext','profile__pro_gsm','profile__pro_trigram','profile__location')
    
    template = loader.get_template('phonebook/phonebook.html')
    context = {
        'colHeaders': ['Firstname LASTNAME',
                       'Entity',
                       'Extension',
                       'Mobile',
                       'Email',
                       'Initials',
                       'Location'],
        'user': user,
    }
    return HttpResponse(template.render(context, request))
  • Here is my template phonebook.html :
{% extends "main/datatables.html" %}

<!-- TABLE TITLE -->
{% block tableTitle %}Phonebook{% endblock %}

<!-- TABLE HEADER -->
{% block tableHeader %}
    {% if colHeaders %}
        {% for header in colHeaders %}
            <th>{{header}}</th>
        {% endfor %}
    {% else %}
        <p>No results</p>
    {% endif %}
{% endblock %}

<!-- TABLE BODY -->
{% block tableBody %}
    {% if user %}
        {% for person in user %}
        <tr>
            <td>{{person.first_name}} {{person.last_name}}</td>
            <td>{{person.profile__entity}}</td>
            <td>{{person.profile__pro_ext}}</td>
            <td>{{person.profile__pro_gsm}}</td>
            <td>{{person.email}}</td>
            <td>{{person.profile__pro_trigram}}</td>
            <td>{{person.profile__location}}</td>
        </tr>  
        {% endfor %}
    {% else %}
        <p>Pas de données.</p>
    {% endif %}
{% endblock %}

<!-- TABLE FOOTER -->
{% block tableFooter %}
    {% if colHeaders %}
        {% for header in colHeaders %}
            <th>{{header}}</th>
        {% endfor %}
    {% else %}
        <p>No results</p>
    {% endif %}
{% endblock %}

Problem :

{{person.profile__entity}}

returns the id of the entity but I'm trying to display the name or alias of the Entity model...

What I've tried :

  1. Reading the Django doc : but as I am not sure of the methods involved, I probably can't find the right section

  2. Dumb things and taking chances in views.py and template like :

    1. {{person.profile__entity_name}}
      
    2. {{person.profile__entity.name}}
      
    3. {{person.profile__entity_entity__name}}
      
  3. Using an approach like with DRF :

    entity = serializers.SlugRelatedField(
            many=False,
            read_only=True,
            slug_field='entity'
        )
    
  4. Tried something like this : https://docs.djangoproject.com/en/4.1/topics/serialization/ but think that I probably missed some steps if it's the right way to do it.

  5. Read posts about issues that sound like mine :

    1. How to access a field from an extended user in Django?

    2. Reverse query on django extended user profile

    3. ...

When using values() searching for a foreignKey returns only the id of the object not the object itself. Add profile__entity__name to your queryset in the views to retrieve the name of the entity.

Back to Top