以下是我的代码的相关部分:
model.py
class Blogger(models.Model):
identity = models.ForeignKey(User, models.SET_NULL, null=True)
def __str__(self):
return self.identity.username
我希望能够使用blogger_instance.identity.username
搜索博客,所以我做了:
admin.py
@admin.register(Blogger)
class BloggerAdmin(admin.ModelAdmin):
list_display = ('__str__', 'identity')
search_fields = ['__str__']
我转到http://127.0.0.1:8000/admin/blog/blogger/
,在搜索字段中输入user
,点击搜索,然后得到 FieldError :
FieldError at /admin/blog/blogger/
Cannot resolve keyword '' into field. Choices are: bio, blog, comment, id, identity, identity_id
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/blog/blogger/?q=user
Django Version: 1.10.6
Exception Type: FieldError
Exception Value:
Cannot resolve keyword '' into field. Choices are: bio, blog, comment, id, identity, identity_id
Exception Location: /Users/sunqingyao/Envs/django_test/lib/python3.6/site-packages/django/db/models/sql/query.py
in names_to_path, line 1327
Python Executable: /Users/sunqingyao/Envs/django_test/bin/python
Python Version: 3.6.0
Python Path:
['/Users/sunqingyao/PycharmProjects/diyblog',
'/Users/sunqingyao/PycharmProjects/diyblog',
'/Users/sunqingyao/Envs/django_test/lib/python36.zip',
'/Users/sunqingyao/Envs/django_test/lib/python3.6',
'/Users/sunqingyao/Envs/django_test/lib/python3.6/lib-dynload',
'/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/sunqingyao/Envs/django_test/lib/python3.6/site-packages']
Server time: Tue, 28 Mar 2017 13:01:58 +0800
当我改变
search_fields = ['__str__']
到
search_fields = ['identity']
我得到了 TypeError :
TypeError at /admin/blog/blogger/
Related Field got invalid lookup: icontains
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/blog/blogger/?q=user
Django Version: 1.10.6
Exception Type: TypeError
Exception Value:
Related Field got invalid lookup: icontains
Exception Location: /Users/sunqingyao/Envs/django_test/lib/python3.6/site-packages/django/db/models/fields/related.py in get_lookup, line 694
Python Executable: /Users/sunqingyao/Envs/django_test/bin/python
Python Version: 3.6.0
Python Path:
['/Users/sunqingyao/PycharmProjects/diyblog',
'/Users/sunqingyao/PycharmProjects/diyblog',
'/Users/sunqingyao/Envs/django_test/lib/python36.zip',
'/Users/sunqingyao/Envs/django_test/lib/python3.6',
'/Users/sunqingyao/Envs/django_test/lib/python3.6/lib-dynload',
'/usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/Users/sunqingyao/Envs/django_test/lib/python3.6/site-packages']
Server time: Tue, 28 Mar 2017 13:12:09 +0800
我应该将search_fields
设置为什么值?
答案 0 :(得分:2)
检查search_fields文档,如上所述
这些字段应该是某种文本字段,例如CharField或 文本域。您还可以在ForeignKey上执行相关查找 ManyToManyField使用查找API“跟随”表示法:
search_fields = ['identity__username']
所以在你的情况下
{{1}}
另请注意,它并没有说您可以使用第一次尝试的计算属性
答案 1 :(得分:1)
像这样编写管理类
#Register your models here.
class BloggerAdmin(admin.ModelAdmin):
list_display = ('__str__', 'identity')
search_fields = ['identity__fieldname', ]
admin.site.register(Blogger, BloggerAdmin)
搜索字段必须与此identity__fieldname