Re: searching stackedinline fields via search_fields in admin

1,786 views
Skip to first unread message
Message has been deleted

ringemup

unread,
Aug 20, 2010, 4:54:36 PM8/20/10
to Django users

The admin search isn't searching the ModelAdmin or Inline files, it's
searching the fields from the model.

You can search a ForeignKey's fields by listing
'foreignkeyfieldname__relatedmodelfieldname' in your search_fields.
So for example if you had the following models:

class Author(models.Model):
...
lastname = models.CharField(max_length=50)


class Book(models.Model):
...
title = models.CharField(max_length=50)
author = models.ForeignKey(Author, related_name='works')


Then you could do the following in your admin:

class BookAdmin(admin.ModelAdmin):
model = Book
search_fields = ['title', 'author__lastname', ...]



However, I'm not sure you can do the reverse:

class AuthorAdmin(admin.ModelAdmin):
model = Author
search_fields = ['lastname', 'works__title']




On Aug 20, 4:45 pm, gondor <condor.c...@gmail.com> wrote:
> Hello All,
>
> I'm wanting to add a field from my stackedinline form to my
> search_fields in admin.  Does anyone know how to do that or point me
> in a general direction/sample code?
>
> Thanx
> Condor

Karen Tracey

unread,
Aug 20, 2010, 9:32:53 PM8/20/10
to django...@googlegroups.com
On Fri, Aug 20, 2010 at 4:54 PM, ringemup <ring...@gmail.com> wrote:
The admin search isn't searching the ModelAdmin or Inline files, it's
searching the fields from the model.

You can search a ForeignKey's fields by listing
'foreignkeyfieldname__relatedmodelfieldname' in your search_fields.
So for example if you had the following models:

class Author(models.Model):
   ...
   lastname = models.CharField(max_length=50)


class Book(models.Model):
   ...
   title = models.CharField(max_length=50)
   author = models.ForeignKey(Author, related_name='works')


Then you could do the following in your admin:

class BookAdmin(admin.ModelAdmin):
   model = Book
   search_fields = ['title', 'author__lastname', ...]



However, I'm not sure you can do the reverse:

class AuthorAdmin(admin.ModelAdmin):
   model = Author
   search_fields = ['lastname', 'works__title']

Yes, you can.

Karen
--
http://tracey.org/kmt/

Condorious

unread,
Aug 21, 2010, 2:01:56 AM8/21/10
to django...@googlegroups.com, django...@googlegroups.com
My goal is to add search to stackedinline in addition to the common standard search fields.  If admin model knows enough to stackinline the right stackedinline model, we should be able to
Search it as well.  I just need someone to point me in the right direction or some me some sample code that does it
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Karen Tracey

unread,
Aug 21, 2010, 8:34:13 AM8/21/10
to django...@googlegroups.com
On Sat, Aug 21, 2010 at 2:01 AM, Condorious <condo...@gmail.com> wrote:
My goal is to add search to stackedinline in addition to the common standard search fields.  If admin model knows enough to stackinline the right stackedinline model, we should be able to
Search it as well.  I just need someone to point me in the right direction or some me some sample code that does it


I am not sure what you are looking for beyond ringmeup's example. That example shows the syntax (double underscore) for listing related fields to be searched in search_fields. This syntax works for both forward and backwards relationships. In the example, Books are presumably listed inline on an Author's admin change page, and the very last ModelAdmin definition includes the title of the author's books among the search fields. So searching on the authors change list page will include searching on a model field that may be listed among its inline information.

There is nothing automatic that will populate search_fields with such fields, if that is what you are looking for. There is also no requirement that the related models for such fields be included as inlines in the model admin, so you could include book titles among the author's search fields even if books were not listed inline on the Author's change page.

Karen
--
http://tracey.org/kmt/

Message has been deleted

gondor

unread,
Aug 21, 2010, 4:49:01 PM8/21/10
to Django users
Here is what i'm trying to do: I've looked at my code and I don't
have a foreign key out reference from my model thus cannot find the
inline fields. reverse reference doens't seem to work either

class CarRadio(modesl.Model):
name = models.ForeignKey('Car');
id = models.CharField(max_length='10', blank=True)


class Car(models.Model):
owner = CharField(max_length='10')
name = CharField(max_length='10')
model = CharField(max_length='10')
typeofwheels = CharField(max_length='10')
...


class CarAdmin(admin.Model):
list_filter = {'model', 'name', 'typeofwheels'} < -- I want to
add list_filter from CarRadioInline here
search_fileds = {'model', 'name',} < -- I wanted to add search
fields from CarRadioInline here
inlines = [ CarRadioInline, ]


class CarRadioInline(admin.StackedInline):
model = CarRadio
extra = 1
fieldset = ((None, {'fields': (('name', 'id'))})

thanx

ringemup

unread,
Aug 22, 2010, 11:04:07 AM8/22/10
to Django users
To add a filter that works with a related field, you have to write a
custom filterspec (which isn't really documented anywhere AFAIK).


To add the search on the related fields you just need to do what I
described above:

class CarAdmin(admin.ModelAdmin)
search_fields = ['model', 'radio_set__name']
...
Reply all
Reply to author
Forward
0 new messages