Let's say I have a model like this:
class Car(models.Model):
BRANDS = (
('FRD', 'Ford'),
('MCD', 'Mercedes'),
...
)
brand = models.CharField(max_length=3, choices=BRANDS)
color = models.CharField(max_length=24)
url = models.URLField()
new = models.BooleanField(default=False)
And would like to generate a list view using class based generic views:
In urls.py
url(r'^car/list/$', CarList.as_view(), name='car_list'),
In views.py
class CarList(ListView):
model = Car
template_name = "list.html"
def get_queryset(self):
return Car.objects.all()
In list.html
{% for car in cars %}
<tr>
<td>{{ car.brand }}</td>
<td>{{ car.color }}</td>
<td>{{ car.url }}</td>
<td>{{ car.new }}</td>
</tr>
Now I would like to offer some options to add filters on the queryset (and sorting options per column). For example a general search box that does a %LIKE% on any column, or a selection from the brand-choices or a simple asc/desc per column.
I know the filters and sorting shall end up in the queryset (get_queryset
) but this requires lots of specific work in the template and view code while I feel there should be some packages that assist in this?
Does anyone have directions for me?