django-hvad icon indicating copy to clipboard operation
django-hvad copied to clipboard

django-hvad compatibility with FilterSet of Django Rest Framework

Open alfredrumss opened this issue 7 years ago • 4 comments

Hi I have an issue when I implement a filter on my ViewSet.. maybe it's something silly but I really don't know.

My ViewSet

class RestaurantViewSet(viewsets.ModelViewSet): queryset = Restaurant.objects.language().all() serializer_class = RestaurantSerializer permission_classes = (permissions.AllowAny, ) filter_backends = (filters.DjangoFilterBackend,) filter_class = RestaurantFilters lookup_field = 'slug' pagination_class = StandardResultsSetPagination

def get_queryset(self):
	user_language = self.request.GET.get('language')
	return Restaurant.objects.language(user_language).all().order_by('-created_at')

my filter:

class RestaurantFilters(FilterSet): category = CharFilter(name='category', lookup_expr='exact')

class Meta:
	model = Restaurant
	fields = ['category']

I think it has to do with something wrong on the get_queryset method.

If someone has implemented something seemed to please help me

alfredrumss avatar Aug 16 '17 04:08 alfredrumss

Hello, Thanks for reporting here! Well, from what you wrote on the other issue, which I assume was intended to end up here, it seems you're using django filter backend. That's the part that is causing issues.

It is because the queryset's actual model is the translation model. It's normal and expected, and works fine everywhere as it behaves exactly like the original model, translations on top of it. However, django filter backend actively tests the model, and bails out.

So I'd say it is not a hvad+DRF issue, but actually a hvad+django-filter issue. Alas, there is no official support of hvad+django-filter at the moment. Most probably it should be possible to make them work together by disabling django-filter's check in some way.

spectras avatar Aug 16 '17 14:08 spectras

Hello, @spectras, django-hvad supports for rest_framework_filters+hvad ?

danilke avatar Aug 17 '17 15:08 danilke

There is no official support. Unfortunately, there are so many third party packages that officially supporting more than one or two would be too much work.

It is worth noting rest_framework_filters is based on django-filters, so it's likely to encounter the same issue. On the positive side, that means making either work should make the other work as well.

I believe the next iteration of django-hvad should work better with queryset-handling modules, because the queryset's model will be the base model and no longer the automatic translations model. That means modules which explicitly check queryset.model would no longer raise an error. I wish they used duck typing instead of explicit checks, that would make this moot.

I didn't try it, but a common workaround for dealing with cases such as django-filter is to:

=> Pass the translation model when they want the model. => Prefix all non-translated fields with master__.

For instance, the following is worth trying:

class RestaurantFilters(FilterSet):
    category = CharFilter(name='master__category', lookup_expr='exact')
    class Meta:
	model = Restaurant._meta.translations_model

Depending on how the 3rd-party package works, this might yield expected results, or this might require some additional filtering (typically on language_code), or this might not work.

The better way would be to create a new contrib glue module in either django-filters or django-hvad, handling the interactions. If someone is interested in contributing that, I'm open to the idea.

spectras avatar Aug 17 '17 15:08 spectras

Thank you, I will try it!

danilke avatar Aug 18 '17 08:08 danilke