Reputation: 16137
Here is the relevant part of my code:
model.py
class Blogger(models.Model):
identity = models.ForeignKey(User, models.SET_NULL, null=True)
def __str__(self):
return self.identity.username
I want to be able to search for a blogger with their blogger_instance.identity.username
, so I did:
admin.py
@admin.register(Blogger)
class BloggerAdmin(admin.ModelAdmin):
list_display = ('__str__', 'identity')
search_fields = ['__str__']
I went to http://127.0.0.1:8000/admin/blog/blogger/
, typed user
in the search field, hit Search, and got an 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
When I change
search_fields = ['__str__']
to
search_fields = ['identity']
I got an TypeError instead:
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
What value should I set search_fields
to?
Upvotes: 6
Views: 2011
Reputation: 412
Write your admin class like this
#Register your models here.
class BloggerAdmin(admin.ModelAdmin):
list_display = ('__str__', 'identity')
search_fields = ['identity__fieldname', ]
admin.site.register(Blogger, BloggerAdmin)
search fields must be like this identity__fieldname
Upvotes: 1
Reputation: 15738
Check search_fields documentation, so as stated there
These fields should be some kind of text field, such as CharField or TextField. You can also perform a related lookup on a ForeignKey or ManyToManyField with the lookup API “follow” notation:
search_fields = ['foreign_key__related_fieldname']
So in your case
search_fields = ['identity__username']
Also notice it doesn't say you could use calculated property as you tried at first
Upvotes: 5