J
J
Jekson2019-07-02 10:10:41
Django
Jekson, 2019-07-02 10:10:41

How to display feedback in django models in template?

There are related models:

class Employee(models.Model):
    """Employee information."""
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='employee', unique=True)
    position = models.CharField("current position in a company", max_length=64, blank=True)
    birth_date = models.DateField("date of birth", null=True)
    skills = models.ManyToManyField(
        Technology, through="Skill", verbose_name="skills", blank=True)

class Technology(models.Model):
    """Technologies."""
    name = models.CharField('technology name', max_length=32, unique=True)

class Skill(models.Model):
    """Information about an employee's skills."""
    LEVELS = (
        ('basic', 'Basic'),
        ('intermediate', 'Intermediate'),
        ('advanced', 'Advanced'),
        ('expert', 'Expert'),
    )
    employee = models.ForeignKey(
        Employee, on_delete=models.CASCADE, related_name="employee_skills")
    technology = models.ForeignKey(Technology, on_delete=models.CASCADE)
    start_date = models.DateField(
        verbose_name='Works with since:')
    level = models.CharField("level", max_length=64, choices=LEVELS)

I have several questions about displaying information on them in the template
1. I 'm trying to display general information on Employee template.html through the User model

{% if request.user.is_authenticated %}
    {{ user.get_full_name }}
    {{ user.employee.position }}
{% endif %}

The first expression - getting the name works, but the problem with the second one is that nothing is displayed. related_name='employee' - added. I try through {{ user.employee_set.position }} - also without result.
And the second question is the output of Skills
. I do it in the same way:
{% for i in  user.employee.employee_skills %}
     {{ i.technology }}
{% endfor %}

and nothing.
I can also display this data through views
class AccountView(TemplateView):

    template_name = "profile.html"

    def get_context_data(self, **kwargs):
        context = super(AccountView, self).get_context_data(**kwargs)
        context['skills'] = Skill.objects.filter(employee__user = self.request.user)
        return context

And it works, but I want to figure out how to get access through User.
Help me figure it out please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-07-02
@Lepilov

{% for i in user.employee.skills.all %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question