I have a filter using ModelChoiceFilter from a DB column called 'semanas' (Weeks) and the values that are stored there at the moment are:
2020-W5 2020-W6
I have the data being displayed in a table (django-tables2), when I select any of these 2 values it does blank like it does not find anything even though I see them displayed in the table.
filters.py
class PagosFilter(django_filters.FilterSet):
semana = django_filters.ModelChoiceFilter(
queryset=Pagos.objects.values_list('semana', flat=True).distinct())
class Meta:
model = Pagos
fields = ['semana', ]
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
tables.py
class PagosTable(tables.Table):
detalles = TemplateColumn(
'<a class="btn btn btn-info btn-sm" href="{% url "pagos_edit" record.id %}">Abrir</a>')
class Meta:
model = Pagos
template_name = "django_tables2/bootstrap-responsive.html"
fields = ('carro', 'pago', 'fecha', 'semana', 'renta')
attrs = {"class": "table table-hover table-sm"}
views.py
class PagosListView(SingleTableMixin, FilterView):
model = Pagos
table_class = PagosTable
template_name = 'AC/payments.html'
filterset_class = PagosFilter
paginate_by = 10
here is an image displaying filter values
Here is an image once one of the values of the filter is selected:
I used to have a value '1' and it worked so I don´t know if the dash in 2020-W06 takes some effect.
Thanks!
I fixed the issue by adding to the model the string representation.
models.py
class Pagos(models.Model):
carro = models.ForeignKey(
Carros, on_delete=models.CASCADE, blank=False, null=False)
pago = models.DecimalField(max_digits=6, decimal_places=2)
fecha = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
semana = models.CharField(max_length=20)
startweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
endweek = models.DateField(
auto_now=False, auto_now_add=False, blank=True, null=True)
renta = models.ForeignKey(
Renta, on_delete=models.PROTECT, blank=False, null=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = "Pagos"
def get_absolute_url(self):
return reverse('pagos')
def __str__(self):
return self.semana