如何制作动态表格

时间:2018-06-07 08:19:19

标签: python django django-forms django-templates

我有不同的产品属性,如尺寸,颜色,品牌等

我想在HTML上的复选框中显示它们以过滤它们

我想动态显示它们 (目前我已将它们硬编码为)

class Att(django_filters.FilterSet):

    q1 = AttributeChoiceValue.objects.filter(attribute__name='color')
    q2 = AttributeChoiceValue.objects.filter(attribute__name='size')

    color = django_filters.ModelMultipleChoiceFilter(
        queryset=q1.values_list('name', flat=True).distinct(),
        widget=forms.CheckboxSelectMultiple)

    size = django_filters.ModelMultipleChoiceFilter(
        queryset=q2.values_list('name', flat=True).distinct(),
        widget=forms.CheckboxSelectMultiple)
    class Meta:
        model = AttributeChoiceValue
        fields = ()

我的模特

class ProductAttribute(models.Model):
    slug = models.SlugField(max_length=50, unique=True)
    name = models.CharField(max_length=100)
    op = models.CharField(max_length=20,default='in')

    class Meta:
        ordering = ('slug', )

    def __str__(self):
        return self.name

    def get_formfield_name(self):
        return slugify('attribute-%s' % self.slug, allow_unicode=True)

    def has_values(self):
        return self.values.exists()



class AttributeChoiceValue(models.Model):
    name = models.CharField(max_length=100)
    slug = models.SlugField(max_length=100)
    attribute = models.ForeignKey(
        ProductAttribute, related_name='values', on_delete=models.CASCADE)

    class Meta:
        unique_together = ('name', 'attribute')

    def __str__(self):
        return self.name

0 个答案:

没有答案