Hi all,
I'd like to search in a m2mfield using the search_fields trick in admin interface (django 1.0.2).
A simple example of models:
"""
class Book(models.Model):
title = models.CharField(...)
authors = models.ManyToManyField('author')
class Author(models.Model):
name = models.CharField(...)
"""
In admin.py, would be great if it could be done simply like this:
"""
class BookAdmin(admin.ModelAdmin):
search_fields = ['title', 'authors']
admin.site.register(models.Book, BookAdmin)
"""
Then, in the search box of the Book page, I could just type an author and search through the m2m field 'authors'. But, doing this way, I got the following error:
Related Field has invalid lookup: icontains
I could achieve this search going to the link
http://localhost/admin/bookstore/book/?authors__name=john , so I still think this is possible.
Another idea could be creating a method in the model that returns a list of authors:
"""
class Book(models.Model):
title = models.CharField(...)
authors = models.ManyToManyField('author')
def get_authors(self):
return authors.all()
class Author(models.Model):
name = models.CharField(...)
"""
admin.py:
"""
class BookAdmin(admin.ModelAdmin):
search_fields = ['title', 'authors',
'get_authors']
admin.site.register(models.Book, BookAdmin)
"""
But then I got the following error message:
Cannot resolve keyword 'get_authors' into field. Choices are: title, authors
So... how can I do this?
Thanks!!
--
João Olavo Baião de Vasconcelos
Bacharel em Ciência da Computação
Analista de Sistemas - Infraestrutura
joaoolavo.wordpress.com