|
AdminExtensions
Current Admin Extensions
You can watch a small demo at Flickr: http://www.flickr.com/photos/jannis/3246408003/ |
► Sign in to add a comment
|
Search
|
|
AdminExtensions
Current Admin Extensions
You can watch a small demo at Flickr: http://www.flickr.com/photos/jannis/3246408003/ |
This is very cool and helpful - so is the Jannis' post in his blog about doing autocompletion on the admin interface.
But could we have some instructions on how to set up and use ForeignKeyAutocompleteAdmin??
In my admin.py I did something like this:
import django_extensions class TravelLogClientAdmin?(django_extensions.admin.ForeignKeyAutocompleteAdmin?):
admin.site.register(TravelLogClient?, TravelLogClientAdmin?)But I fail to understand how to configure my app to properly serve the django-extensions media...
in my case all I had to do was to put the django_extensions/media folder in my usual media location..
I found it difficult to figure out how to install and use this extension (the demo at flicker seems to be gone). So here are some step-by-step instructions based on what worked for me.
ForeignKeyAutocompleteAdmin
Django Command Extensions includes a new ModelAdmin class called ForeignKeyAutocompleteAdmin which can be used to add a search function to ForeignKey fields on Admin pages. Here is how to set up and use it.
Example models for Authors and Books:
from django.db import models class Author(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) class Book(models.Model) title = models.CharField(max_length=100) author = models.ForeignKey(Author)Install Django Command Extensions.
Make sure that the Django Extensions media folder is available in the MEDIA_ROOT directory of your Django settings.py.
Add searching for Authors to the admin of the Book model by extending the new ForeignKeyAutocompleteAdmin? class and adding a dictionary called related_search_fields where the key is the ForeignKey and the value is a list of fields to search on in Author model:
from django.contrib import admin from django_extensions.admin import ForeignKeyAutocompleteAdmin class BookAdmin(ForeignKeyAutocompleteAdmin): related_search_fields = { 'author': ('first_name', 'last_name') } admin.site.register(Book, BookAdmin)This creates an Admin interface with two text input fields, separated by a search icon [M]. Text entered in the first field is used to search for matching Author objects. The second field contains the primary key id of the selected object. The search icon opens a popup window to do the searching, if preferred. On the line the text fields are instructions on how to use them.
Author: ______ [M] _____ Use the left field to do Author lookups in the fields first_name and last_name.Author: M? Use the left field to do Author lookups in the fields first_name and last_name.