DjangoAdmin自定义过滤器
class UserIDFilter(admin.SimpleListFilter): # 自定义用户查询过滤器 title = _('关联用户') parameter_name = 'user_id' def lookups(self, request, model_admin): params = UserProfile.objects.all().values_list('nickname', 'user_id') look_choice = [] for nickname, user_id in list(params): temp = ('{}'.format(user_id), _('{}'.format(nickname))) look_choice.append(temp) return look_choice def queryset(self, request, queryset): user = self.value() if not user: return queryset return queryset.filter(user=user)
Ideal are like the stars --- we never reach them ,but like mariners , we chart our course by them