def test_product_list_view(user: UserFactory, api_request_factory: APIRequestFactory):
view = ProductListView.as_view()
# Make an authenticated request to the view...
# Урл можно писать любой
request = api_request_factory.get('/api/v1/products/')
force_authenticate(request, user=user)
response = view(request)
assert response.status_code == 200
class ProductFilter(django_filters.FilterSet):
brand = django_filters.ModelMultipleChoiceFilter(field_name='brand__name',
to_field_name='name',
label='Бренды',
queryset=Brand.objects.all()
)
def __init__(self, *args, **kwargs):
slug = kwargs.pop('slug', None)
super(ProductFilter, self).__init__(*args, **kwargs)
self.filters['brand'].queryset = Brand.objects.filter(is_active=True,
product_brand__isnull=False,
product_brand__category__slug=slug),
widget=forms.CheckboxSelectMultiple)
class Meta:
model = Product
fields = ['brand']