J
J
Jekson2019-08-21 17:35:44
Django
Jekson, 2019-08-21 17:35:44

How to filter queryset in template?

There are
techgroup models

class Techgroup(models.Model):
    """ Group of technology """

    name = models.CharField('group_name', max_length=32, unique=True)

technology
class Technology(models.Model):
    """Technologies."""
    tech_set = models.ForeignKey(Skillset, on_delete=models.CASCADE, related_name="skillset")
    name = models.CharField('technology name', max_length=32, unique=True)
    group = models.ForeignKey(Techgroup, on_delete=models.CASCADE, related_name="group")

employee
class Employee(models.Model):
    """Employee information."""
    user = models.OneToOneField(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='employee')
    position = models.ManyToManyField(
        Position, related_name='position', blank=True)
    birth_date = models.DateField("date of birth", blank=True, null=True)
    summary = models.TextField("summary", blank=True, default='')
    skills = models.ManyToManyField(
        Technology, through="Skill", verbose_name="skills", blank=True)

I pass them to the context
class AccountView(AuthorizedMixin, TemplateView):

    template_name = "profile.html"

    def get_context_data(self, **kwargs):
        context = super(AccountView, self).get_context_data(**kwargs)
        context['employee'] = Employee.objects.get(pk=self.kwargs['pk'])
        context['skillset'] = Skillset.objects.all()
        context['techgroup'] = Techgroup.objects.all()
        return context

I can't figure out how to filter technologies by groups and display them in a template
. I have instances
of techgroup: language, framework and technology: python, django, java
,
flask

{% for group in techgroup.all %}
                  {{ group.name }}
            {% for skill in employee.employee_skills.all %}
                {% if skill.technology.group.name =   ????????  %}
                       {{ {{skill.technology.name}} }}
                 {% endif %}
            {% endfor %}

  {% endfor %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-08-21
@Lepilov

Templates are not meant for logic, do filtering in views.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question