Django custom admin url + changelog for custom list filter by tags

In django admin I wanted to set up my own tag filter (tags are injected with django-tagging )

I created a ModelAdmin for this and it worked great by adding a custom urlconf and changing the view of the changelog. It should work with urls like: http://127.0.0.1:8000/admin/reviews/review/only-tagged-vista/

But now I get an "invalid literal for int () with base 10:" only-tagged-vista ", an error that means it keeps a match with the review edit page instead of the custom filter page, and I can't figure out why it worked and I can’t find what change would affect this.

Any help was appreciated.

Relevant code:

class ReviewAdmin(VersionAdmin):

    def changelist_view(self, request, extra_context=None, **kwargs): 
        from django.contrib.admin.views.main import ChangeList 
        cl = ChangeList(request, self.model, list(self.list_display), 
                        self.list_display_links, self.list_filter, 
                        self.date_hierarchy, self.search_fields,  
                        self.list_select_related, 
                        self.list_per_page, 
                        self.list_editable, 
                        self) 
        cl.formset = None

        if extra_context is None: 
            extra_context = {}

        if kwargs.get('only_tagged'): 
            tag = kwargs.get('tag')
            cl.result_list = cl.result_list.filter(tags__icontains=tag) 
            extra_context['extra_filter'] = "Only tagged %s" % tag

        extra_context['cl'] = cl
        return super(ReviewAdmin, self).changelist_view(request, extra_context=extra_context)

    def get_urls(self): 
        from django.conf.urls.defaults import patterns, url 
        urls = super(ReviewAdmin, self).get_urls()

        def wrap(view):
            def wrapper(*args, **kwargs):
                return self.admin_site.admin_view(view)(*args, **kwargs)
            return update_wrapper(wrapper, view)

        info = self.model._meta.app_label, self.model._meta.module_name 
        my_urls = patterns('', 
                # make edit work from tagged filter list view
                # redirect to normal edit view
                url(r'^only-tagged-\w+/(?P<id>.+)/$',
                    redirect_to,
                    {'url': "/admin/"+self.model._meta.app_label+"/"+self.model._meta.module_name+"/%(id)s"}
                    ),
                # tagged filter list view
                url(r'^only-tagged-(P<tag>\w+)/$',  
                    self.admin_site.admin_view(self.changelist_view), 
                     {'only_tagged':True}, name="changelist_view"), 
        ) 

        return my_urls + urls

      

Edit: The original issue has been fixed.

Now I get "Unable to filter request after slice has been made." for the line:

cl.result_list = cl.result_list.filter(tags__icontains=tag)

      

I'm not sure where this list of results is sliced ​​before applying the tag filter.

Edit2: This is because of self.list_per_page in the ChangeList declaration. However, the correct solution has not yet been found. Temperature correction:

        if kwargs.get('only_tagged'):
            list_per_page = 1000000
        else:
            list_per_page = self.list_per_page 
        cl = ChangeList(request, self.model, list(self.list_display), 
                        self.list_display_links, self.list_filter, 
                        self.date_hierarchy, self.search_fields,  
                        self.list_select_related, 
                        list_per_page, 
                        self.list_editable, 
                        self) 

      

+2


source to share


3 answers


You don't have a question mark in front of the P in '^only-tagged-(P<tag>\w+)/$'

, so the expression won't match.



+4


source


In the above code example, get_urls () is aligned so that it is not part of the ReviewAdmin class, but rather a separate function. I guess this might lead to your problem if you have the same path in a real source.



0


source


The error occurs in tags with multiple words because you are only matching one word tag. this works: r '^ only-tagged - (? P [^ /] +) / $'

0


source







All Articles