Django randomly order users in admin
I am trying to create a Django admin filter that will get random groups of users. At this point, I have two problems:
- Applying a custom filter to the User model, and
- 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 sug开发者_如何学Gogestions?
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?
It should be fairly easy to do, just create a ModelAdmin
with a ordering
property.
Something like this should do:
class UserAdmin(ModelAdmin):
ordering = ('?',)
精彩评论