Ticket #5407: many2manywidget.diff
File many2manywidget.diff, 4.2 KB (added by , 17 years ago) |
---|
-
media/js/admin/RelatedObjectLookups.js
19 19 function dismissRelatedLookupPopup(win, chosenId) { 20 20 var name = win.name.replace(/___/g, '.'); 21 21 var elem = document.getElementById(name); 22 if (elem.className.indexOf('v RawIdAdminField') != -1 && elem.value) {22 if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) { 23 23 elem.value += ',' + chosenId; 24 24 } else { 25 25 document.getElementById(name).value = chosenId; -
options.py
168 168 if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)): 169 169 if isinstance(db_field, models.ForeignKey) and db_field.name in self.raw_id_fields: 170 170 kwargs['widget'] = widgets.ForeignKeyRawIdWidget(db_field.rel) 171 return db_field.formfield(**kwargs)172 171 else: 173 # Wrap the widget's render() method with a method that adds 174 # extra HTML to the end of the rendered output. 175 formfield = db_field.formfield(**kwargs) 176 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site) 177 return formfield 172 if isinstance(db_field, models.ManyToManyField) and db_field.name in self.raw_id_fields: 173 kwargs['widget'] = widgets.ManyToManyRawIdWidget(db_field.rel) 174 kwargs['help_text'] = '' 175 # Wrap the widget's render() method with a method that adds 176 # extra HTML to the end of the rendered output. 177 formfield = db_field.formfield(**kwargs) 178 formfield.widget.render = widgets.RelatedFieldWidgetWrapper(formfield.widget.render, db_field.rel, self.admin_site) 179 return formfield 178 180 179 181 # For any other type of field, just call its formfield() method. 180 182 return db_field.formfield(**kwargs) -
widgets.py
3 3 """ 4 4 5 5 from django import newforms as forms 6 from django.utils.datastructures import MultiValueDict 6 7 from django.utils.text import capfirst 7 8 from django.utils.translation import ugettext as _ 8 9 from django.conf import settings … … 75 76 url = '?' + '&'.join(['%s=%s' % (k, v) for k, v in self.rel.limit_choices_to.items()]) 76 77 else: 77 78 url = '' 78 attrs['class'] = 'vRawIdAdminField' # The JavaScript looks for this hook. 79 if not attrs.has_key('class'): 80 attrs['class'] = 'vForeignKeyRawIdAdminField' # The JavaScript looks for this hook. 79 81 output = [super(ForeignKeyRawIdWidget, self).render(name, value, attrs)] 80 82 # TODO: "id_" is hard-coded here. This should instead use the correct 81 83 # API to determine the ID dynamically. … … 85 87 return u''.join(output) 86 88 #if self.change: # TODO 87 89 #output.append(' <strong>TODO</strong>') 90 91 class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): 92 """ 93 A Widget for displaying ManyToMany ids in the "raw_id" interface rather than 94 in a <select multiple> box. 95 """ 96 def __init__(self, rel, attrs=None): 97 super(ManyToManyRawIdWidget, self).__init__(rel, attrs) 98 99 def render(self, name, value, attrs=None): 100 attrs['class'] = 'vManyToManyRawIdAdminField' 101 if value: 102 value = ','.join(value) 103 else: 104 value = "" 105 return super(ManyToManyRawIdWidget, self).render(name, value, attrs) 88 106 107 def value_from_datadict(self, data, files, name): 108 if isinstance(data, MultiValueDict): 109 return data[name].split(',') 110 return data.get(name, None) 111 89 112 class RelatedFieldWidgetWrapper(object): 90 113 """ 91 114 This class is a wrapper whose __call__() method mimics the interface of a