Есть две взаимосвязанные модели
class Skill(models.Model):
"""Information about an employee's skills."""
LEVELS = (
('basic', 'Basic'),
('intermediate', 'Intermediate'),
('advanced', 'Advanced'),
('expert', 'Expert'),
)
YEAR_CHOICES = []
for r in range(1, 11):
YEAR_CHOICES.append((r, r))
employee = models.ForeignKey(
Employee, on_delete=models.CASCADE, related_name="employee_skills")
technology = models.ForeignKey(Technology, on_delete=models.CASCADE)
year = models.IntegerField(verbose_name='Duration of technology use (years)', choices=YEAR_CHOICES, default=1)
level = models.CharField("level", max_length=64, choices=LEVELS)
def __str__(self):
return '{} is {} in {}'.format(
self.employee.full_name, self.level, self.technology.name)
class Project(models.Model):
"""Project information."""
name = models.CharField("project name", max_length=64)
description = models.TextField("project description")
technologies = models.ManyToManyField(
Technology, verbose_name="technologies used on the project")
def __str__(self):
return self.name
filters.pyclass EmployeeFilter(django_filters.FilterSet):
skills = django_filters.ModelChoiceFilter(queryset=Technology.objects.all())
class Meta:
model = Employee
fields = ['skills']
template.html<form method="get">
<div class="card-body">
<div class="row">
<div class="form-group w-100">
<label for="skills">Technology</label>
{% render_field filter.form.skills class="form-control bg-light" %}
</div>
</div>
<button type="submit" class="btn btn-warning">
<span ><i class="fas fa-search"></i></span> Search
</button>
<button type="button" class="btn btn-secondary"><span ><i class="fas fa-recycle fa-fw"></i></i></span><a href="{% url 'account_list' %}" class="text-white">Clear</a></button>
</div>
</form>
В таком виде поиск по skills работает , т.е я получаю отфильтрованных пользователей с выбранными значениями из модели Technology, но мне надо вытянуть из модели Skills еще и level , и вот с этим сложности. Модель пользователя содержит связь с моделью Technology
class Employee(models.Model):
"""Employee information."""
user = models.OneToOneField(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='employee')
skills = models.ManyToManyField(
Technology, through="Skill", verbose_name="skills", blank=True)
Как достучаться до значения нужного поля в Skills?