【问题标题】:Django admin: use one-to-one relationship in search_fieldsDjango admin:在 search_fields 中使用一对一的关系
【发布时间】:2013-04-12 21:56:03
【问题描述】:

我正在尝试使用以下 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

有什么解决办法吗?

【问题讨论】:

    标签: django django-admin django-modeladmin


    【解决方案1】:

    尝试使用user__username,根据lookup API “follow” notation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2011-12-11
      • 2012-10-20
      • 2011-08-07
      • 1970-01-01
      • 2010-11-18
      • 2017-05-01
      相关资源
      最近更新 更多