I
I
Iskander Mammadov2015-03-07 17:58:34
Django
Iskander Mammadov, 2015-03-07 17:58:34

How to create a filter for objects displayed through the ListView?

The ListView displays all objects in the University model.
The University model has a city field.
How to output only university with a specific city value with help?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Django, 2015-03-07
@winordie

class UniversityListView(ListView):
    ...
    def get_queryset(self):
        university = University.objects.filter(city='London')
        return university

If a:
class University(models.Model):
    ...
    city = models.ForeignKey('City')
    ...

then
class UniversityListView(ListView):
    ...
    def get_queryset(self):
        city = City.objects.get(name='London')
        university = University.objects.filter(city=city)
        return university

J
jkrieger, 2015-03-31
@jkrieger

Look at QSortFilterProxyModel.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question