Skip to content

Commit

Permalink
Fix ForeignkeyAutocompleteAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
comogo authored and justinabrahms committed Dec 4, 2009
1 parent ed41647 commit 312d445
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions django_extensions/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.utils.encoding import smart_str
from django.utils.translation import ugettext as _
from django.utils.text import get_text_list
from django.utils.functional import update_wrapper

from django_extensions.admin.widgets import ForeignKeySearchInput

Expand All @@ -27,7 +28,7 @@ class ForeignKeyAutocompleteAdmin(admin.ModelAdmin):
have to be represented by autocomplete input, together with
a list of target model fields that are searched for
input string, e.g.:
related_search_fields = {
'author': ('first_name', 'email'),
}
Expand All @@ -41,16 +42,26 @@ class ForeignKeyAutocompleteAdmin(admin.ModelAdmin):
related_search_fields = {}
related_string_functions = {}

def __call__(self, request, url):
if url is None:
pass
elif url == 'foreignkey_autocomplete':
return self.foreignkey_autocomplete(request)
return super(ForeignKeyAutocompleteAdmin, self).__call__(request, url)
def get_urls(self):
from django.conf.urls.defaults import patterns, url

def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args, **kwargs)
return update_wrapper(wrapper, view)

info = self.model._meta.app_label, self.model._meta.module_name

urlpatterns = patterns('',
url(r'foreignkey_autocomplete/$',
wrap(self.foreignkey_autocomplete),
name='%s_%s_autocomplete' % info),
) + super(ForeignKeyAutocompleteAdmin, self).get_urls()
return urlpatterns

def foreignkey_autocomplete(self, request):
"""
Searches in the fields of the given related model and returns the
Searches in the fields of the given related model and returns the
result as a simple string to be used by the jQuery Autocomplete plugin
"""
query = request.GET.get('q', None)
Expand Down Expand Up @@ -112,7 +123,7 @@ def formfield_for_dbfield(self, db_field, **kwargs):
Overrides the default widget for Foreignkey fields if they are
specified in the related_search_fields class attribute.
"""
if (isinstance(db_field, models.ForeignKey) and
if (isinstance(db_field, models.ForeignKey) and
db_field.name in self.related_search_fields):
model_name = db_field.rel.to._meta.object_name
help_text = self.get_help_text(db_field.name, model_name)
Expand All @@ -123,3 +134,4 @@ def formfield_for_dbfield(self, db_field, **kwargs):
kwargs['help_text'] = help_text
return super(ForeignKeyAutocompleteAdmin,
self).formfield_for_dbfield(db_field, **kwargs)

0 comments on commit 312d445

Please sign in to comment.