Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django-hvad compatibility with FilterSet of Django Rest Framework #340

Open
alfredrumss opened this issue Aug 16, 2017 · 4 comments
Open

Comments

@alfredrumss
Copy link

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

@spectras
Copy link
Collaborator

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.

@danilke
Copy link

danilke commented Aug 17, 2017

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

@spectras
Copy link
Collaborator

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.

@danilke
Copy link

danilke commented Aug 18, 2017

Thank you, I will try it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants