我正在使用django-filter,我有两个模型CustomUser和Shop. 如何更改过滤器选择查询集以便用户(request.user)只能过滤他的商店?
用户
class CustomUser(AbstractBaseUser, PermissionsMixin):
shop = models.ManyToManyField(Shop, blank=True, related_name='custom_user')
店铺
class Shop(models.Model):
address = models.CharField(_('Address'), unique=True, max_length=64, blank=False, null=False, db_index=True)
过滤器.py
shops = Shop.objects.filter(is_active=True)
SHOP_CHOICES = [('All', 'All')]
for x in shops:
SHOP_CHOICES.append((x.address, x))
SHOP_CHOICES = tuple(SHOP_CHOICES)
class ShopFilter(django_filters.FilterSet):
address = django_filters.MultipleChoiceFilter(choices=SHOP_CHOICES)
class Meta:
model = Shop
fields = ['address']
视图.py
f = ShopFilter(request.GET)
尚方宝剑之说
相关分类