Django admin:在search_fields中使用一对一的关系

时间:2013-04-19 13:27:43

标签: django django-admin django-modeladmin

我正在尝试使用以下Model和ModelAdmin类向我的模型管理列表页面添加搜索:

models.py

from django.contrib.auth.models import User

class UserProfile(models.Model):
        user = models.OneToOneField(User)
        country = CountryField(blank=True, null=True)

admin.py

from django.contrib import admin
from models import UserProfile

class UserProfileAdmin(admin.ModelAdmin):
        list_display = ('user','country')
        search_fields = ['user']

但是在尝试访问管理面板中的UserProfile时出现以下错误:

 at /admin/profiles/userprofile/ Related Field has invalid
 lookup: icontains

我也尝试了以下内容:

search_fields = ['user_username']

并且

search_fields = ['user_name']
    def user_name(self,obj):
        return obj.user.username

任何解决方案?

1 个答案:

答案 0 :(得分:7)

根据lookup API “follow” notation

尝试使用user__username