[Answered ]-Employee model Linked to Django User

1👍

You are using ForeignKey to link Employee to User, so from User instance you can access a QuerySet of all related Employee objects (using related_name you provided). Like that:

{% for employee in request.user.employees.all %}
    {{ employee.title }} {{ employee.firstname }}
{% endfor %}

If you plan to have exactly one Employee object for one User object, it’s better to use:

user = models.OneToOneField(User, on_delete=models.CASCADE)

because then you can easily get Employee field values:

{{ request.user.employee.title }} {{ request.user.employee.firstname }}

Leave a comment