使用UserProfile字段的Django 1.4 User Admin list_filter [英] Django 1.4 User Admin list_filter using UserProfile field

查看:50
本文介绍了使用UserProfile字段的Django 1.4 User Admin list_filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新了代码以反映在查询集过滤器中的一个错误,其中我有user__user_type的用户已被替换为正确的userprofile__user_type.

I updated the code to reflect one mistake in the queryset filter, where I had user__user_type, has been replaced with the correct userprofile__user_type.

我正在使用Django 1.4,并且我了解有一个新功能可为管理员创建自定义list_filters,它取代了FilterSpec API.

I'm using Django 1.4, and I understand there's a new feature to create custom list_filters for the admin, that replaced the FilterSpec API.

我已经阅读了无数有关创建自定义admin list_filters的SO帖子,但是我仍然很困惑.

I've read countless SO posts about creating custom admin list_filters, but I'm still stuck.

我的用例是引用UserProfile模型中的字段的User对象的list_filter.

My use case is for a list_filter for my User objects that's referencing a field in the UserProfile Model.

因此在models.py中:

So in models.py:

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)
    user_type = models.CharField(max_length=25, choices=USER_TYPES, default='Client')
    ...

以及在admin.py中:

and in admin.py:

from django.contrib import admin
from django.contrib.admin import site, ModelAdmin, ChoicesFieldListFilter
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from models import UserProfile

class UserTypeFilter(ChoicesFieldListFilter):
    title = 'User Type'
    parameter_name = 'user_type'

    def lookups(self, request, model_admin):
        usertypes = set([c.user_type for c in UserProfile.objects.all()])
        return [(c.id, c.user_type) for c in usertypes]

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(userprofile__user_type=self.value())
        else:
            return queryset

class UserAdmin(UserAdmin):
    list_filter = ('is_staff', UserTypeFilter)
    inlines = (UserProfileInline, )


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

这是我得到的错误:

'UserAdmin.list_filter[1]' is 'UserTypeFilter' which is of type FieldListFilter but is not associated with a field name.

我最初尝试使用SimpleListFilter,但出现此错误

I originally tried using the SimpleListFilter, and got this error

'unicode' object has no attribute 'id'

这很有意义,因为我的user_type字段是一个选择字段,而这些选择是unicode对象.

Which makes sense, because my user_type field is a choice field, and the choices are unicode objects.

文档显示了此示例:

class PersonAdmin(UserAdmin):
    list_filter = ('company__name',)

但这似乎意味着'company'是User模型上的一个字段.我是否需要重新定义用户模型以包括用于UserProfile的OneToOneField?还是我缺少一些明显的方式来引用用户的个人资料?

But this seems to imply that 'company' is a field on the User model. Do I need to redefine my User model to include a OneToOneField for UserProfile? Or am I missing some obvious way to reference a user's profile?

谢谢

推荐答案

您引用的第一个错误似乎是 FieldListFilter 所特有的,我从未使用过.我想这是要自动化与特定字段相关的内容.

The first error you reference seems to be specific to FieldListFilter which I've never used. I guess the point of it is to automate something related to a specific field.

我认为您的示例没有用,因为您提供了所有选项.

I see no use for it with your example as you're supplying all options.

我现在只使用 SimpleListFilter ...

I'd just use a SimpleListFilter for now...

无论如何,您的错误是 usertypes 是一组 c.user_type ,它是一个字符串.它没有 id 属性.

Anyways your error is that usertypes is a set of c.user_type which is a string. It has no id attribute.

您需要返回 [(在用户类型中为c的c,c)] ,其中第一个值是传递给QS的值,第二个是显示值.

You need to return [(c, c) for c in usertypes] where the first value is the value passed to your QS, and the second is the display value.

其余部分无需更改.

这篇关于使用UserProfile字段的Django 1.4 User Admin list_filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆