Using jquery’s autocomplete in django admin: how about inlines?

I still haven’t solved this problem entirely, and I’ll tell you why. Well the issue has been addressed by many, and among them probably the first and most influential solution is Jannis Leidel’s ‘An autocomplete form widget for ForeignKey model fields’.

Leidel says:

[..] my question is, if this could be done on a more abstract level for enhancing relations. when having a foreignkey, you get the browse-icon (lens) right near the input-field. it´d be awesome to search the relations just by typing something into the field … the autocomplete-functionality should then search the related entries on the basis of the defined search-fields.

Here’s a nice screencast of the the result [UPDATE: unfortunately the screencast is not available anymore]:

Picture 1

Leidel’s solution is not a copy&paste type of thing, however it’s very useful to study his code so to understand what’s going on there, and then replicate it in your specific user-scenario. That’s exactly what several people have done.

Nonetheless, reusing is more agile that doing things from scratch, isn’t it? So, after a bit of research I reached the conclusion that the most interesting and reusable (meaning, plug-and-play) django implementations of jquery’s autocomplete are two:

  1. the admin extensions section of the django-extensions project on google code
  2. PROs: it extends django’s raw_input widget, so while you can make use of the autocomplete you can still click on the lens and select the object you want from a new objects-list window (see screencast below). Also, you can enter the id of the object and the autocomplete will fill in straightaway the human-readable form of it (not sure how useful it is, but I like it)
    CONs: supports only one-2-many relationships, that is, it doesn’t work with many2many or inlines

  3. another project on google code called – surprise – django-autocomplete !
  4. PROs: very compact (just one file, no new apps to include). Works with one2many AND many2many
    CONs: again, no support for inlines…

    After having a go with both of them I decided to use the first one [the admin extensions section of the django-extensions project], cause I don’t need many2many support and I really liked the fact that the standard ‘raw-id’ behaviour is still available. Here’s a nice screencast (by the way I just realized that this one has been made by Jannis Leidel too, so I guess it’s sort of an evolution of the previous one) [UPDATE #2: also this screencast is not available anymore! what’s going on with mr Leidel?]:

    Picture 3

    Conclusion: there’s only one BIG problem – it doesn’t work with inlines out-of-the-box ! I’m looking at extending it now… will keep you posted!



One Response to “Using jquery’s autocomplete in django admin: how about inlines?”

[…] in Django #2 Posted in programming by magicrebirth on October 12, 2009 In a previous post I was talking about using autocomplete fields in django’s admin, and how this feature was […]

Autocomplete in Django #2 « Parerga und Paralipomena added these pithy words on Oct 12 09 at 12:30 pm