我有两个型号
class Sku(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
class Manufacturer(models.Model):
title = models.CharField()
我希望在过滤时只出现与当前sku相关的制造商。
my view part:
c['skus'] = self.object.skus.filter(hide=False, prices__price_type=PRICE_ROZN).prefetch_related('prices',
'stock').all().order_by(
'prices__value')
sku_filter = SkuFilter(self.request.GET, c['skus'])
如果此时自我存在,我会以这种方式过滤掉制造商:
class SkuFilter(django_filters.FilterSet):
# manufacturer__title = django_filters.CharFilter(lookup_expr='icontains')
manufacturer = django_filters.filters.ModelMultipleChoiceFilter(
name='manufacturer',
to_field_name='title',
queryset=Manufacturer.objects.filter(
pk__in=self.queryset.objects.values_list('manufacturer').distinct()),
)
class Meta:
model = Sku
fields = ['manufacturer', ]
但很明显,在特定的时刻,自我还不存在。
答案 0 :(得分:0)
我用这种方法解决了这个问题:
创建没有过滤的字段,如(Manufacturer.objects.all())
等待元类魔术创建base_fitler字段
覆盖 init 并替换当前过滤器
let finalList = []
this.state.categories.forEach( (cat, index) => {
finalList.push(<tr...>{this.state.category}</tr>)
this.state.data.forEach( (row, index) => {
if(row.category === cat){
finalList.push(
<tr key={i}>
<td className="col-lg-2 text-center">{row.name}</td>
<td className="col-lg-2 text-center">{row.alias}</td>
<td className="col-lg-2 text-center">{row.description}</td>
<td className="col-lg-1 text-center">{row.default_value}</td>
<td className="col-lg-1 text-center">{row.min_value}</td>
<td className="col-lg-1 text-center">{row.max_value}</td>
<td className="col-lg-1 text-center">Action</td>
</tr>
)
}
})
})