Skip to content

Commit

Permalink
Resolves #14370 -- Adds select2 widget for foreign keys in admin
Browse files Browse the repository at this point in the history
Adds jQuery Select2 version 4 to support async select inputs
including a search feature.

Changes:
- only str representation is supported at this point
- the search feature uses the same field as the model admin
  • Loading branch information
codingjoe committed Apr 3, 2016
1 parent 7d485d5 commit 17343cd
Show file tree
Hide file tree
Showing 63 changed files with 7,477 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
@@ -1,3 +1,4 @@
**/{*.min,jquery}.js
**/*.min.js
**/vendor/**/*.js
django/contrib/gis/templates/**/*.js
node_modules/**.js
10 changes: 10 additions & 0 deletions django/contrib/admin/options.py
Expand Up @@ -19,6 +19,7 @@
NestedObjects, flatten_fieldsets, get_deleted_objects,
lookup_needs_distinct, model_format_dict, quote, unquote,
)
from django.contrib.admin.widgets import AdminForeignKeyWidget
from django.contrib.auth import get_permission_codename
from django.core.exceptions import (
FieldDoesNotExist, FieldError, PermissionDenied, ValidationError,
Expand Down Expand Up @@ -97,6 +98,7 @@ class IncorrectLookupParameters(Exception):
class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
"""Functionality common to both ModelAdmin and InlineAdmin."""

autocomplete_fields = ()
raw_id_fields = ()
fields = None
exclude = None
Expand Down Expand Up @@ -213,6 +215,14 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
Get a form Field for a ForeignKey.
"""
db = kwargs.get('using')

if db_field.name in self.autocomplete_fields:
related_admin = self.admin_site._registry.get(db_field.remote_field.model)
if not related_admin.search_fields:
msg = 'The admin for %s needs to implement "search_fields".'
raise NotImplementedError(msg % db_field.remote_field.model)
kwargs['widget'] = AdminForeignKeyWidget(model_admin=self, using=db)

if db_field.name in self.raw_id_fields:
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.remote_field,
self.admin_site, using=db)
Expand Down
2 changes: 2 additions & 0 deletions django/contrib/admin/sites.py
Expand Up @@ -3,6 +3,7 @@
from django.apps import apps
from django.conf import settings
from django.contrib.admin import ModelAdmin, actions
from django.contrib.admin.views.main import ForeignKeyJsonView
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.db.models.base import ModelBase
Expand Down Expand Up @@ -239,6 +240,7 @@ def wrapper(*args, **kwargs):
url(r'^jsi18n/$', wrap(self.i18n_javascript, cacheable=True), name='jsi18n'),
url(r'^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$', wrap(contenttype_views.shortcut),
name='view_on_site'),
url(r'^foreignkey_json/$', wrap(ForeignKeyJsonView.as_view(admin_site=self)), name='fk_json')
]

# Add in each model's views, and create a list of valid URLS for the
Expand Down
21 changes: 21 additions & 0 deletions django/contrib/admin/static/admin/css/vendor/select2/LICENSE.md
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2012-2015 Kevin Brown, Igor Vaynberg, and Select2 contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

0 comments on commit 17343cd

Please sign in to comment.