Filter with first item of a Manytomany Django

Asked

Viewed 147 times

3

I have it in my template:

{% for j in jobs %}
    <tbody>
        {% if j.background.all.0.active == False %}
        ...

Hence I need to filter the view similar to this.

I tried to

jobs = jobs.filter(background__first__active=False)

But I know it’s completely wrong.

The background is a Manytomany that contains a field called active, but I just want to get the first item.

How do I make this filter?

  • 1

    One of the options that exist besides Meumodel.objects.all() and Meumodel.objects.filter(), is Meumodel.objects.first(). If you want the first element of a queryset, you can use it in the template {{ j.background.first }}. Only this takes the first item of a queryset, I did not understand exactly the usefulness of this filter that you made.

2 answers

1


1

Corroborating Regis' response, you can do it as follows:

jobs = Jobs.objects.filter(background_active=False)[0]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.