django-filter over models

36 views
Skip to first unread message

René L. Hechavarría

unread,
Sep 13, 2018, 3:26:20 AM9/13/18
to django...@googlegroups.com
Hello,

I have a model, Jobs and Persons, Jobs are listed in ListView, i need to add a filter (django-filter), if i filter over field in Jobs all work good, but i need to filter also for Persons. this is and example

@property
def get_persons(self):
return self.persons_all.all()

That allow to add persons to Jobs model in de html. How i filter over persons in Jobs view?

Thanks in advance.



Mateusz

unread,
Sep 18, 2018, 11:15:02 PM9/18/18
to Django users
In that answer I assume Jobs are connected anyhow with Persons with a ForeignKey field like so:

File /appname/models.py:
from django.db import models


class Job(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Person(models.Model):
    name = models.CharField(max_length=100)
    surname = models.CharField(max_length=100)

    job = models.ForeignKey(Job, on_delete=models.SET_NULL, null=True, default=None)

    def __str__(self):
        return self.name + ' ' + self.surname

File /appname/views.py:
from django.shortcuts import render
from django.views.generic.list import ListView

from appname.models import Job, Person


class JobsWithPersonsView(ListView):
queryset = Job.objects.all()
template_name = 'jobs_list.html'

File /appname/templates/jobs_list.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Jobs</title>
</head>
<body>
{% for job in object_list %}
{{ job.name }}
{% for person in job.person_set.all %}
{{ person.name }} {{ person.surname }}
{% empty %}
Nobody works there.
{% endfor %}
{% empty %}
No jobs.
{% endfor %}
</body>
</html>

And others you probably know how to deal with...
File /urls.py:
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('appname/', include('appname.urls'))
]

File /appname/urls.py:
from django.urls import path
from appname import views

urlpatterns = [
path('jobslist/', views.JobsWithPersonsView.as_view(), name='jobslist')
]

BTW: Your model names should be in singular form, if you want to be able to use plural forms in some cases, use verbose_name_plural (docs).

Mateusz

unread,
Sep 18, 2018, 11:47:52 PM9/18/18
to Django users
Sorry, my previous answer was senseless, I realized that just after adding it.

1) You can filter jobs in appname/views.py:
queryset = Job.objects.filter(name__contains="job")

2) and people in template /appname/templates/jobs_list.html:
{% for job in object_list %}
    <h1>{{ job.name }}</h1>
    {% for person in job.person_set.all %}
        {% if person.name|length <= 4 %}
{{ person.name }} {{ person.surname }}
{% endif %}

{% empty %}
Nobody works there.
{% endfor %}

I still find that 2) inefficient, but could do anything else. Read something about registering filters that I do not understand that well, sorry.

brajesh kumar

unread,
Sep 19, 2018, 10:44:20 PM9/19/18
to Django users
I think this answers my question , trying to implement it.
Reply all
Reply to author
Forward
0 new messages