我正在尝试使用以下 Model 和 ModelAdmin 类向我的模型管理列表页面添加搜索:
模型.py
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
country = CountryField(blank=True, null=True)
管理员.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
有什么解决办法吗?