user1428660
user1428660

Reputation:

Limiting a querset for a field in Django-Filter

I have a django-filter FilterSet that takes a queryset of a user's saved links. Tags is a ManyToManyField, and I'd like to narrow that selection down to tags created (owned) by the same user. Completely at a loss - the docs aren't that extensive.

class LinkFilter(django_filters.FilterSet):

    class Meta:
        model = Link
        fields = ['title', 'domain', 'tags', 'date', 'modified']

https://github.com/alex/django-filter/

Upvotes: 2

Views: 1804

Answers (4)

NINSIIMA WILBER
NINSIIMA WILBER

Reputation: 169

Just need to override the property form method, and then customize your uqeryset based on your conditions

class LinkFilter(django_filters.FilterSet):

@property
def form(self):
    form = super(LinkFilter, self).form
    form.fields['yourField'].queryset = form.fields['yourField'].queryset.filter(yourFilterCondition)

    return form

class Meta:
    model=Link
    fields='__all__'

Upvotes: 1

KevinS
KevinS

Reputation: 8627

Here is how i did it for a very similar situation (tried to adapt to your case) :

class LinkFilter(django_filters.FilterSet):

    def __init__(self, *args, **kwargs):
        current_user = kwargs['current_user']
        del kwargs['current_user']
        super(LinkFilter, self).__init__(*args, **kwargs)
        self.filters['customer'].extra.update(
            {'queryset': models.Tags.objects.get_my_tags(current_user)})

    class Meta:
        model = Link
        fields = ['title', 'domain', 'tags', 'date', 'modified']

in your view :

def home(request):
    f = LinkFilter(request.GET, queryset=models.Links.objects.get_my_links(request.user), current_user=request.user)
    return render_to_response('public/links.html', {'filter': f})

with get_my_tags and get_my_links defined in my managers.py file.

Upvotes: 1

schillingt
schillingt

Reputation: 13731

I haven't used django-filter before, but looking at the docs you should be able to do something like below. You'll need to pass the user you want to filter on in the constructor for LinkFilter for the keyword argument user. Otherwise it will continue to show all tags from all users. I also guessed that the User ForeignKey property on the Link class was named created_by.

class LinkFilter(django_filters.FilterSet):
    class Meta:
        model = Link
        fields = ['title', 'domain', 'tags', 'date', 'modified']

    def __init__(self, data=None, queryset=None, prefix=None, strict=None, user=None):
        super(LinkFilter, self).__init__(data=data, queryset=queryset, prefix=prefix, strict=strict))
        if user:
            self.filters['tags'].filter(created_by=user)

Upvotes: 1

vadimchin
vadimchin

Reputation: 1497

class ProductFilter(FilterSet):
    def __init__(self,data=None, queryset=None, prefix=None, strict=None, **kwargs):
        self.kwargs = kwargs
        super(ProductFilter, self).__init__(data, queryset, prefix, strict)

    @property
    def form(self):
        form = super(ProductFilter, self).form

        for key, queryset in self.kwargs.iteritems():
            form.fields[key].queryset = queryset

        return form

    class Meta:
        model = Product
        fields = ['name', 'tags']

class ViewFilter(TemplateView):
    template_name = 'frontend/view_filter.html'
    def get_context_data(self, **kwargs):
        context = super(ViewFilter, self).get_context_data(**kwargs)

        context['filter'] = ProductFilter(self.request.GET,
                                          queryset=Product.objects.filter(tags__user_id=2),
                                          tags=Tag.objects.filter(user_id=2))

        return context

Upvotes: 0

Related Questions