mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
newforms-admin: Fixed #5407. Javascript problems with raw_id fields. Thanks Christian Metts.
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@6310 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ccd8fb0d85
commit
c3fe6d805a
1
AUTHORS
1
AUTHORS
@ -201,6 +201,7 @@ answer newbie questions, and generally made Django that much better:
|
|||||||
mattycakes@gmail.com
|
mattycakes@gmail.com
|
||||||
Jason McBrayer <http://www.carcosa.net/jason/>
|
Jason McBrayer <http://www.carcosa.net/jason/>
|
||||||
mccutchen@gmail.com
|
mccutchen@gmail.com
|
||||||
|
Christian Metts
|
||||||
michael.mcewan@gmail.com
|
michael.mcewan@gmail.com
|
||||||
mikko@sorl.net
|
mikko@sorl.net
|
||||||
Slawek Mikula <slawek dot mikula at gmail dot com>
|
Slawek Mikula <slawek dot mikula at gmail dot com>
|
||||||
|
@ -19,7 +19,7 @@ function showRelatedObjectLookupPopup(triggeringLink) {
|
|||||||
function dismissRelatedLookupPopup(win, chosenId) {
|
function dismissRelatedLookupPopup(win, chosenId) {
|
||||||
var name = win.name.replace(/___/g, '.');
|
var name = win.name.replace(/___/g, '.');
|
||||||
var elem = document.getElementById(name);
|
var elem = document.getElementById(name);
|
||||||
if (elem.className.indexOf('vRawIdAdminField') != -1 && elem.value) {
|
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
|
||||||
elem.value += ',' + chosenId;
|
elem.value += ',' + chosenId;
|
||||||
} else {
|
} else {
|
||||||
document.getElementById(name).value = chosenId;
|
document.getElementById(name).value = chosenId;
|
||||||
|
@ -168,13 +168,17 @@ class BaseModelAdmin(object):
|
|||||||
if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
|
if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
|
||||||
if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
|
if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields:
|
||||||
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel)
|
kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel)
|
||||||
return db_field.formfield(**kwargs)
|
|
||||||
else:
|
else:
|
||||||
# Wrap the widget's render() method with a method that adds
|
if isinstance(db_field, models.ManyToManyField) and db_field.name in self.raw_id_fields:
|
||||||
# extra HTML to the end of the rendered output.
|
kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel)
|
||||||
formfield = db_field.formfield(**kwargs)
|
kwargs['help_text'] = ''
|
||||||
|
# Wrap the widget's render() method with a method that adds
|
||||||
|
# extra HTML to the end of the rendered output.
|
||||||
|
formfield = db_field.formfield(**kwargs)
|
||||||
|
# Don't wrap raw_id fields. Their add function is in the popup window.
|
||||||
|
if not db_field.name in self.raw_id_fields:
|
||||||
formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site)
|
formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site)
|
||||||
return formfield
|
return formfield
|
||||||
|
|
||||||
# For any other type of field, just call its formfield() method.
|
# For any other type of field, just call its formfield() method.
|
||||||
return db_field.formfield(**kwargs)
|
return db_field.formfield(**kwargs)
|
||||||
|
@ -3,6 +3,7 @@ Form Widget classes specific to the Django admin site.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from django import newforms as forms
|
from django import newforms as forms
|
||||||
|
from django.utils.datastructures import MultiValueDict
|
||||||
from django.utils.text import capfirst
|
from django.utils.text import capfirst
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -75,7 +76,8 @@ class ForeignKeyRawIdWidget(forms.TextInput):
|
|||||||
url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in self.rel.limit_choices_to.items()])
|
url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in self.rel.limit_choices_to.items()])
|
||||||
else:
|
else:
|
||||||
url = ''
|
url = ''
|
||||||
attrs['class'] = 'vRawIdAdminField' # The JavaScript looks for this hook.
|
if not attrs.has_key('class'):
|
||||||
|
attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook.
|
||||||
output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
|
output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)]
|
||||||
# TODO: "id_" is hard-coded here. This should instead use the correct
|
# TODO: "id_" is hard-coded here. This should instead use the correct
|
||||||
# API to determine the ID dynamically.
|
# API to determine the ID dynamically.
|
||||||
@ -85,6 +87,27 @@ class ForeignKeyRawIdWidget(forms.TextInput):
|
|||||||
return u''.join(output)
|
return u''.join(output)
|
||||||
#if self.change: # TODO
|
#if self.change: # TODO
|
||||||
#output.append(' <strong>TODO</strong>')
|
#output.append(' <strong>TODO</strong>')
|
||||||
|
|
||||||
|
class ManyToManyRawIdWidget(ForeignKeyRawIdWidget):
|
||||||
|
"""
|
||||||
|
A Widget for displaying ManyToMany ids in the "raw_id" interface rather than
|
||||||
|
in a <select multiple> box.
|
||||||
|
"""
|
||||||
|
def __init__(self, rel, attrs=None):
|
||||||
|
super(ManyToManyRawIdWidget, self).__init__(rel, attrs)
|
||||||
|
|
||||||
|
def render(self, name, value, attrs=None):
|
||||||
|
attrs['class'] = 'vManyToManyRawIdAdminField'
|
||||||
|
if value:
|
||||||
|
value = ','.join(value)
|
||||||
|
else:
|
||||||
|
value = ""
|
||||||
|
return super(ManyToManyRawIdWidget, self).render(name, value, attrs)
|
||||||
|
|
||||||
|
def value_from_datadict(self, data, files, name):
|
||||||
|
if isinstance(data, MultiValueDict):
|
||||||
|
return data[name].split(',')
|
||||||
|
return data.get(name, None)
|
||||||
|
|
||||||
class RelatedFieldWidgetWrapper(object):
|
class RelatedFieldWidgetWrapper(object):
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user