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
<https://docs.djangoproject.com/en/2.1/ref/models/options/#verbose-name-plural>
).
W dniu czwartek, 13 września 2018 05:26:20 UTC+2 użytkownik René L.
Hechavarría napisał:
>
> 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.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/0c30f699-8dda-43fb-89e2-33e2c8100e4b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.