Queryset doesn't update when filtered

Hi,

Following this tutorial (starting at 7’05), I’m trying to implement a facet filter based navigation within my Django project. Here is my code:

models.py

class ObjetArchi(models.Model):
    id = models.AutoField(
        primary_key=True,
        )  
    materiau = models.ForeignKey(
        'Materiau',
        on_delete=models.SET_NULL,
        related_name='objets_archi',
        blank=True,
        null=True
        )

class Materiau(models.Model);
    id = models.AutoField(
        primary_key=True
        )
    nom = models.CharField(
        max_length=125,
        blank=True,
        null=True
        )     

views.py

class NoticesListView(ListView):
    model = ObjetArchi
    template_name = 'notices/index.html'
    context_object_name = 'liste_notices'
    paginate_by = 20

    def get_queryset(self):
        qs = super().get_queryset()
        materiau = self.request.GET.get('materiau__nom')
        if materiau:
            qs = qs.filter(type=materiau)
        return qs

    def get_context_data(self, **kws):
        context = super().get_context_data(**kws)
        context['materiaux'] = (
            self.get_queryset()
            .values('materiau__nom')
            .annotate(count=Count('id'))
        )
        return context

urls.py

urlpatterns = [
    path('', views.NoticesListView.as_view(), name='notices_list'),
]

notices/index.html

{% for materiau in materiaux %}
    <ul>
        <a href="?materiau={{ materiau.materiau__nom }}">
        {{ materiau.materiau__nom }} ({{ materiau.count }})
    </ul>
{% endfor %}

{% if liste_notices %}
    <ul>
    {% for notice in liste_notices %}
        <li><a href="{% url 'notices:detail' notice.id %}">{{ notice.titre }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>Aucune notice n'est accessible.</p>
{% endif %}

The facet shows up with the expected materiau__nom and count, but the queryset doesn’t update when I click on the filter categories. Is there any visible error in the code?

Thanks for your insights!

The name of your query variable is defined as materiau:

But your view is looking for a different variable name:

Hi Ken,

Thanks for your suggestion.

I’ve tried to align both names before (either by setting both to materiau or to materiau__nom) but it always gets me the same “Field error” error message:

FieldError at /

Cannot resolve keyword 'type' into field. Choices are: (...) materiau, materiau_id, (...)```

Specifically, that error is telling you that type is not the name of a column in your ObjetArchi model.

Of course! Thanks Ken. As usual when I’m stuck like that, I was focusing too much on the part I thought was causing the issue, and missing what was clearly stated in front of my eyes.

The view now works this way:

def get_queryset(self):
  qs = super().get_queryset()
  materiau = self.request.GET.get('materiau')
  if materiau:
      qs = qs.filter(materiau__nom=materiau)
  return qs

And is displayed this way:

{% for materiau in materiaux %}
    <ul>
        <a href="?materiau={{ materiau.materiau__nom }}">
        {{ materiau.materiau__nom }} ({{ materiau.count }})
    </ul>
{% endfor %}