I have a project in which I need to filter elements by fecha_separacion
, by default it starts on the current date but I need to change that date through a form.
This is my model ( models.py
):
class Agenda(MarcadorTiempo):
# otros campos
fecha_separacion = models.DateField(null=False, blank=False, verbose_name='Fecha de Separación')
And this is my Form:
class FechaForm(forms.Form):
fecha = forms.DateField(required=False)
This is my view( views.py
):
class AgendaGeneralListView(ListView):
model = Agenda
paginate_by = 100
template_name = 'agendas/agenda_general/agenda_general.html'
context_object_name = 'agendas'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = FechaForm
return context
def get_queryset(self):
queryset = super(AgendaGeneralListView, self).get_queryset()
return queryset.filter(fecha_separacion=date.today())
As you can see, I'm currently filtering on the current date, but I'd like to be able to change this to the date I submit through the form.
Should I send the request
as an argument to the method get_queryset
or how can I do this?
This is the template:
<form action="" method="get">
<div class="row">
<div class="col-sm-4">
<div class="widget-body">
<div class="widget-main">
<label for="id-date-picker-1">Fecha De Agenda</label>
<div class="row">
<div class="col-xs-8 col-sm-11">
<div class="input-group">
<input class="form-control date-picker" name="fecha" id="id-date-picker-1" type="text" data-date-format="dd-mm-yyyy" />
<span class="input-group-addon">
<i class="fa fa-calendar bigger-110"></i>
</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="widget-body">
<div class="widget-main">
<button type="submit" value="Crear" class="btn btn-primary">
<i class="ace-icon fa fa-filter bigger-120"></i>Filtrar
</button>
</div>
</div>
</div>
</div>
</form>
I solved it by writing the method
get_queryset
as follows