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-tagging integration #17

Closed
tehfink opened this issue Oct 16, 2009 · 8 comments
Closed

django-tagging integration #17

tehfink opened this issue Oct 16, 2009 · 8 comments

Comments

@tehfink
Copy link

tehfink commented Oct 16, 2009

hi alex, great project!
i've been trying various things for a while, but i can't seem to find a decent solution to filter tags from django-tagging with django-filter.
have you considered this? could you point me in the right direction, code-wise?

@alex
Copy link
Contributor

alex commented Oct 16, 2009

The right solution is to make a "TagFilter", I might write up a prototype if I have the time, but basically look at some of the Filters inlcuded in django-filter now for an example of what it might look like.

@tehfink
Copy link
Author

tehfink commented Oct 17, 2009

cool, thanks.

so far, i copied the AllValuesFilter and extended the MultipleChoiceFilter:

class TagFilter(MultipleChoiceFilter):
    @property
    def field(self):
    #qs = self.model._default_manager.distinct().order_by(self.name).values_list(self.name, flat=True)
        #returns list of unicode strings, similar to above
        qs = self.model.tags.split()
        self.extra['choices'] = [(o, o) for o in qs]
        return super(TagFilter, self).field

this displays properly, but will i have to modify BaseFilterSet to deal with tag matching?

@alex
Copy link
Contributor

alex commented Oct 17, 2009

Now you need to override the filter() method on TagFilter, to do the actual filtering. If you look at the parameters it gets it should be obvious what you need to do :)

@tehfink
Copy link
Author

tehfink commented Oct 18, 2009

thanks for your help, alex!
ok, this seems to work on a model field that has been registered with 'tagging.register':

class TagFilter(MultipleChoiceFilter):
    @property
    def field(self):
        qs = self.model.tags.split()
        self.extra['choices'] = [(o, o) for o in qs]
        return super(TagFilter, self).field

    def filter(self, qs, value):        
        value_flat = ' '.join(value)
        if value_flat:
            return qs.model.tagged.with_all(value_flat)
        else:
            return qs

unfortunately, it doesn't play nice with the other fields in the FilterSet (which are ignored); i'm assuming because there is no qs.filter(**… ? is it possible for tagged.with_all to work with a qs.filter? or maybe i'm going about this the wrong way.

@alex
Copy link
Contributor

alex commented Oct 18, 2009

Yeah, filter should return a QuerySet that is just the one passed to it filtered down. Perhaps the best bet is to make the tag filter the first filter on your FilterSet.

@tehfink
Copy link
Author

tehfink commented Oct 18, 2009

ah, i missed this bit in the django-tagging docs:

``with_all(tags, queryset=None)`` -- creates a ``QuerySet`` containing model instances which are tagged with *all* the given tags. If a ``queryset`` argument is provided, it will be used as the basis for the resulting ``QuerySet``.

http://code.google.com/p/django-tagging/source/browse/trunk/docs/overview.txt

changing the relevant line in TagFilter to the following seems to work:

return qs.model.tagged.with_all(value_flat, qs)

cool. thanks for your help!

@xordoquy
Copy link
Contributor

xordoquy commented Feb 7, 2013

I'm not sure it's a good thing to include this tag application since it hasn't been updated for the last 2 years.
Maybe taggit would be a wiser choice.

@carltongibson
Copy link
Owner

I'm going to close this one for now. If someone wants to put together a pull request with a TagFilter implementation I'd be happy to review.

(I'm slightly cautious about adding integrations with every-and-other Django apps out there — it seems like a never ending task — but certainly some cases may be justified and having examples to guide implementations would be good.)

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

No branches or pull requests

4 participants