我正在尝试使用以下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
任何解决方案?