Add filters to the Users List/Page in the admin app

54 views
Skip to first unread message

Rishabh Manocha

unread,
Jun 11, 2008, 5:30:22 AM6/11/08
to django...@googlegroups.com
Hey all,

I'm using django's in-built authentication mechanism for my app. Several of my models are of the form:

class UserTechSkill(models.Model):
    user = models.ForeignKey(User,edit_inline = models.TABULAR)
    skill = models.ForeignKey(TechSkillsList,core=True)
    prof_level = models.ForeignKey(ProficiencyLevel,core=True)
    years = models.IntegerField("Years of Experience", max_length=2)
   
    def __str__(self):
        return u'%s' % ';'.join([str(self.skill),str(self.prof_level)])

TechSkillsList is a list that is updated via the admin app and displayed to the user using a select multiple box (the user cannot alter the list of TechSkills).

Now, the django admin app provides (by default) a page where user details are listed. I do not want to change that list. However, I would like to add filters on that page (remove the superuser status and staff status filters and add others like TechSkillsList etc.). I was wondering if this were possible, and if so, how. I am using a (somewhat out of date) trunk checkout of the djago code. If this is not possible in the current admin, is it possible using newforms-admin??

Many Thanks,

Rishabh

Rajesh Dhawan

unread,
Jun 12, 2008, 9:30:36 AM6/12/08
to Django users
Hi Rishabh,

>
> Now, the django admin app provides (by default) a page where user details
> are listed. I do not want to change that list. However, I would like to add
> filters on that page (remove the superuser status and staff status filters
> and add others like TechSkillsList etc.). I was wondering if this were
> possible, and if so, how. I am using a (somewhat out of date) trunk checkout
> of the djago code. If this is not possible in the current admin, is it
> possible using newforms-admin??

Yes, newsforms-admin will let you create an admin area for the
auth.User model using your own admin options (list filters, search,
etc.) It's worth switching to that branch and trying this out.

http://code.djangoproject.com/wiki/NewformsAdminBranch
http://code.djangoproject.com/wiki/NewformsHOWTO

-Rajesh D

Rishabh Manocha

unread,
Jun 13, 2008, 7:47:48 AM6/13/08
to django...@googlegroups.com
This is what I was thinking, but I thought there was a glimmer of hope of achieving this without moving over to newforms-admin. Is there absolutely no way to do this using trunk??
 
I'll give newforms-admin a shot on my dev box over the weekend and see what comes out of it. I will research this too, but are there any backwards incompatible changes between the current trunk and the newforms-admin branch?? I'm thinking the qs-rf merge maybe introduced some issues...
 
Thanks anyways,
 
R

 
--
Best,

Rishabh

Rajesh Dhawan

unread,
Jun 13, 2008, 10:53:13 AM6/13/08
to Django users
Hi Rishabh,

> This is what I was thinking, but I thought there was a glimmer of hope of
> achieving this without moving over to newforms-admin. Is there absolutely no
> way to do this using trunk??

There is:

1. You can tweak the django.contrib.auth models.py file in your local
installation i.e. tweak list_filters to your needs in User.Admin inner
class there.

2. You can disable the auth application, make a copy of it in your own
Python path, say /my/pythonapps/auth and tweak it there.

3. You can "monkey patch" the list_filters option on the Admin inner
class This is highly ugly but won't require you to change the Django
Admin class. Basically, in one of your root level Python modules'
__init__.py insert this:

from django.contrib.auth.models import User
User._meta.admin.list_filter = None # <--- or replace with whatever
filter you want

>
> I'll give newforms-admin a shot on my dev box over the weekend and see what
> comes out of it. I will research this too, but are there any backwards
> incompatible changes between the current trunk and the newforms-admin
> branch?? I'm thinking the qs-rf merge maybe introduced some issues...

Apart from the admin classes being specified in a different way and
the URLs.py syntax for including admin being different, there should
be no backward incompatible changes. The newforms-admin (nfa) branch
fairly frequently imports latest trunk changes. Here's the last merge
from trunk to nfa: (http://code.djangoproject.com/changeset/7604) In
particular, all qs-rf changes from trunk are already in nfa.

Hope this helps you decide.

-Rajesh

M.Ganesh

unread,
Jun 13, 2008, 7:25:51 PM6/13/08
to django...@googlegroups.com
Hi Rajesh,

Is it possible for present admin and nf-admin to co-exist?

i.e I change my apps/models to nf-admin one-by-one. So a app/model will
have old admin until it is changed to nf-admin

Is it possible?

Thanks in advance

Regards Ganesh

Rajesh Dhawan

unread,
Jun 13, 2008, 7:35:22 PM6/13/08
to Django users
lps you decide.
>
> Hi Rajesh,
>
> Is it possible for present admin and nf-admin to co-exist?

No.

>
> i.e I change my apps/models to nf-admin one-by-one. So a app/model will
> have old admin until it is changed to nf-admin
>
> Is it possible?

You could instead use the following snippet to convert your old admin
options to nfa Admin classes:

http://www.djangosnippets.org/snippets/603/

I haven't used that snippet personally but it looks like, at a
minimum, it will save you loads of time even if it doesn't emit the
perfect nfa Admin classes.

-RD

Rishabh Manocha

unread,
Jun 24, 2008, 12:19:29 AM6/24/08
to django...@googlegroups.com
Ok, so I need a bit more direction here. I've got newforms-admin setup and working as far as displaying the various FK relationships a user has on his/her page. However, I'm still not able to figure out how to display FK relationships in the list_filter page. Here is my setup (the UserTechSkill from above has not changed, except for the admin stuff):

Note: I haven't copied and pasted here - don't have my work laptop with me right now - so there might be some typos, but the logic is the same.

in models.py:

from django.contrib.auth.admin import UserAdmin

...

class MyUserAdmin(UserAdmin):
    inlines = [UserTechSkillsAdmin,...]
    list_filter = ('is_staff',)

admin.site.unregister(User)
admin.site.register(User,MyUserAdmin)

Now what I would like to be able to do is be able to add a TechSkillsList filter to the list_filter tuple. I figure that since a user is related to the UserTechSkill class and a UserTechSkill class is related to the TechSkillsList class, there must be some way I can get a list of TechSkillsList in the User class (a list of TechSkillsList which includes skills chosen by all users, but not the ones that haven't been selected by anyone yet). Can someone point me in the right direction here??

Best,

Rishabh

Rishabh Manocha

unread,
Jun 25, 2008, 9:50:03 PM6/25/08
to django...@googlegroups.com
Anybody got any ideas here?? I could really use some help with this problem.

Thanks,

Rishabh

Rajesh Dhawan

unread,
Jun 26, 2008, 10:02:54 AM6/26/08
to Django users
Hi Rishabh,

On Jun 24, 12:19 am, "Rishabh Manocha" <rmano...@gmail.com> wrote:
> Ok, so I need a bit more direction here. I've got newforms-admin setup and
> working as far as displaying the various FK relationships a user has on
> his/her page. However, I'm still not able to figure out how to display FK
> relationships in the list_filter page. Here is my setup (the UserTechSkill
> from above has not changed, except for the admin stuff):
>
> Note: I haven't copied and pasted here - don't have my work laptop with me
> right now - so there might be some typos, but the logic is the same.
>
> in models.py:

class UserTechSkill(models.Model):
user = models.ForeignKey(User,edit_inline = models.TABULAR)
skill = models.ForeignKey(TechSkillsList,core=True)
prof_level = models.ForeignKey(ProficiencyLevel,core=True)
years = models.IntegerField("Years of Experience", max_length=2)


def __str__(self):
return u'%s' %
';'.join([str(self.skill),str(self.prof_level)])



>
> from django.contrib.auth.admin import UserAdmin
>
> ...
>
> class MyUserAdmin(UserAdmin):
> inlines = [UserTechSkillsAdmin,...]
> list_filter = ('is_staff',)
>
> admin.site.unregister(User)
> admin.site.register(User,MyUserAdmin)
>
> Now what I would like to be able to do is be able to add a TechSkillsList
> filter to the list_filter tuple. I figure that since a user is related to
> the UserTechSkill class and a UserTechSkill class is related to the
> TechSkillsList class, there must be some way I can get a list of
> TechSkillsList in the User class (a list of TechSkillsList which includes
> skills chosen by all users, but not the ones that haven't been selected by
> anyone yet). Can someone point me in the right direction here??

Unfortunately, there's no *easy* way in newforms-admin to filter by a
field that doesn't existly directly in the model in question (the
'User' model in your case.)

It is possible to create a custom FilterSpec but the way that's wired
into the admin ChangeList screen requires the filter to be on a field
that is defined in the model. Since UserTechSkill is not a field in
User, MyUserAdmin can't have a list_filter on it much less on
UserTechSkill.skill.

You could create your own view for a custom change list screen for
your user model. That way, in your own, template you would be able to
display any custom filters you like. Clicking on a change list item
could be linked to the existing admin add/update URL for that user
object.

-Rajesh D


Reply all
Reply to author
Forward
0 new messages