views:

29

answers:

2

I am trying to create a Django admin filter that will get random groups of users. At this point, I have two problems:

  1. Applying a custom filter to the User model, and
  2. Displaying a random set of users.

On #1, I've tried using User.username.random_filter = True, but it comes back with an AttributeError saying that User has no attribute username.

On #2, I know I can get 50 random users with User.objects.order_by('?')[:50], but I have not been able to figure out how to get the result of such a query to show up in the admin listing. As far as I can tell, the listing is generated by the URL's GET request, but I've not had any luck ordering with it.

Any suggestions?

A: 

If I were you (and I am), I would stop trying to integrate this functionality with the Django admin site. Speaking from experience, you'll find that what you're trying to do is much easier to implement as regular views. Sure, it isn't be as pretty, but something that works beats something that's pretty but doesn't work, right?

Colorado
A: 

It should be fairly easy to do, just create a ModelAdmin with a ordering property.

Something like this should do:

class UserAdmin(ModelAdmin):
    ordering = ('?',)
WoLpH
Will that make it always random?
Colorado
@Colorado: yes, as long as you don't manually order by something else.
WoLpH
I'm sorry, I meant "Won't that make it always random?" I want to be able to order it normally most of the time, but only have it sorted randomly upon request.
Colorado
@Colorado: the Django admin does not offer that possibility. You can order by any visible field manually but for the rest you can only change the default sort.
WoLpH