(3rd party app) How to safely reverse a custom user model admin url?

85 views
Skip to first unread message

Ivan VenOsdel

unread,
Oct 25, 2013, 8:39:59 PM10/25/13
to django...@googlegroups.com
My project uses a custom user model and I am trying to use a 3rd party app that uses the Reversing Admin URLs technique to enhance my administration interface.

However problems arise when the app tries to make use of the url template tag to do a reverse lookup on the auth user ModelAdmin instance. When 'auth_user_(something)' is referenced directly the template will produce a NoReverseMatch error.

I would like to produce a patch for the package maintainer but I am unsure of how to reference the stand in user model without tying the app to my project or doing something kludgy that goes against the spirit of the django url tag.

The specific app I have been working with is the django-blog-zinnia application. Zinnia is using the following technique to provide changeset details in a generic way through the Django admin.

The problem occurs when it tries to reverse auth_user_* when the site is using a custom user model. For Zinnia it occurs in the following places.

I have already reported this issue to the Zinnia maintainer however I would like to provide something more meaningful in the way of a patch. Replacing the auth_user part of the offending lines with 'myapp_customuser' will fix the issue for me but how do I do something that is viable for a 3rd party application?

Thanks!

Ivan VenOsdel

unread,
Oct 28, 2013, 5:15:39 PM10/28/13
to django...@googlegroups.com
No takers? Maybe I am giving everyone a case of TL;DR.

More simply, how do I put {{ app_label }}_{{ model_name }}_changelist in a template for a custom auth_user model without knowing the details of said model? 

I am guessing I don't want to refer to settings.auth_user_model in the template. It feels like the only way is to use the tag in that case and instead somehow call the changelist function (wherever that is) in the view. If so, anyone know how to do that?

Mike Dewhirst

unread,
Oct 29, 2013, 12:31:44 AM10/29/13
to django...@googlegroups.com
On 29/10/2013 8:15am, Ivan VenOsdel wrote:
> More simply, how do I put {{app_label}}_{{model_name}}_changelist in a
> template for a custom auth_user model without knowing the details of
> said model?
>
> I am guessing I don't want to refer to settings.auth_user_model in the
> template. It feels like the only way is to use the tag in that case and
> instead somehow call the changelist function (wherever that is) in the
> view. If so, anyone know how to do that?
>

You could either make a custom tag which behind the scenes can do (more
or less) anything or write a method or class for your view which returns
a dict or list or queryset.

I would decide which to use based on whether the result is mostly
display oriented or mostly business logic oriented. Business logic goes
in the view and display tweaks go in custom tags.

For a custom tag check
https://docs.djangoproject.com/en/1.5/howto/custom-template-tags/

The view code would be something like ...

from django.template import RequestContext
from django.shortcuts import render_to_response
from wherever import special_logic # returns list or queryset

def display_whatever(request, arg):
rqcontext = RequestContext(request)
content = { whatever: special_logic(arg), }
return render_to_response('whatever.html', content, rqcontext)

hth

Mike
Reply all
Reply to author
Forward
0 new messages