带有ModelChoice值的Django-filter和Django-tables2过滤器未过滤

问题描述 投票:0回答:1

我从一个名为'semanas'(Weeks)的数据库列中使用ModelChoiceFilter进行过滤,目前存储在其中的值是:

2020-W52020-W6

我将数据显示在表(django-tables2)中,当我选择这2个值中的任何一个时,它都会显示为空白,因为即使我看到它们显示在表中也找不到任何东西。

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

这里是显示过滤器值的图像

enter image description here

这里是选择过滤器值之一的图像:

enter image description here

我以前的值是'1',并且可以正常工作,所以我不知道2020-W06的破折号是否会起作用。

谢谢!

django django-models django-filter django-tables2
1个回答
0
投票

我通过将字符串表示形式添加到模型中解决了该问题。

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
© www.soinside.com 2019 - 2024. All rights reserved.