Ticket #9071: add-r9751.diff
File add-r9751.diff, 5.3 KB (added by , 16 years ago) |
---|
-
django/contrib/admin/options.py
39 39 filter_horizontal = () 40 40 radio_fields = {} 41 41 prepopulated_fields = {} 42 exclude_add = [] 42 43 43 44 def formfield_for_dbfield(self, db_field, **kwargs): 44 45 """ … … 138 139 # formfield can be None if it came from a OneToOneField with 139 140 # parent_link=True 140 141 if formfield is not None: 141 formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site) 142 add = db_field.name not in self.exclude_add 143 formfield.widget = widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field.rel, self.admin_site, add) 142 144 return formfield 143 145 144 146 # For any other type of field, just call its formfield() method. -
django/contrib/admin/widgets.py
196 196 This class is a wrapper to a given widget to add the add icon for the 197 197 admin interface. 198 198 """ 199 def __init__(self, widget, rel, admin_site ):199 def __init__(self, widget, rel, admin_site, add=True): 200 200 self.is_hidden = widget.is_hidden 201 201 self.needs_multipart_form = widget.needs_multipart_form 202 202 self.attrs = widget.attrs 203 203 self.choices = widget.choices 204 204 self.widget = widget 205 205 self.rel = rel 206 self.add = add 206 207 # so we can check if the related object is registered with this AdminSite 207 208 self.admin_site = admin_site 208 209 … … 222 223 related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()) 223 224 self.widget.choices = self.choices 224 225 output = [self.widget.render(name, value, *args, **kwargs)] 225 if rel_to in self.admin_site._registry: # If the related object has an admin interface: 226 # if the related object has an admin interface and we can show it 227 if self.add and rel_to in self.admin_site._registry: 226 228 # TODO: "id_" is hard-coded here. This should instead use the correct 227 229 # API to determine the ID dynamically. 228 230 output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \ -
tests/regressiontests/admin_widgets/models.py
48 48 >>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime 49 49 >>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget 50 50 >>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper 51 >>> from django.contrib.admin import site 52 >>> from django.forms.widgets import Select as SelectWidget 51 53 52 54 Calling conditional_escape on the output of widget.render will simulate what 53 55 happens in the template. This is easier than setting up a template and context … … 116 118 >>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden) 117 119 >>> print w.render('test', child_of_hidden.parent_id, attrs={}) 118 120 <input type="text" name="test" value="93" class="vForeignKeyRawIdAdminField" /><a href="../../../admin_widgets/inventory/?t=barcode" class="related-lookup" id="lookup_id_test" onclick="return showRelatedObjectLookupPopup(this);"> <img src="%(ADMIN_MEDIA_PREFIX)simg/admin/selector-search.gif" width="16" height="16" alt="Lookup" /></a> <strong>Hidden</strong> 121 122 # Check that RelatedFieldWidgetWrapper doesn'tshow the add image when explicitely requested 123 >>> site.register(Band) 124 >>> rel = Album._meta.get_field('band').rel 125 >>> w = RelatedFieldWidgetWrapper(SelectWidget(), rel, site, True) 126 >>> print w.render('test', 'test') 127 <select name="test"> 128 </select><a href="../../../admin_widgets/band/add/" class="add-another" id="add_id_test" onclick="return showAddAnotherPopup(this);"> <img src="/media/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a> 129 130 >>> w = RelatedFieldWidgetWrapper(SelectWidget(), rel, site, False) 131 >>> print w.render('test', 'test') 132 <select name="test"> 133 </select> 134 119 135 """ % { 120 136 'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX, 121 137 'STORAGE_URL': default_storage.url(''), -
docs/ref/contrib/admin.txt
224 224 ``birth_date``, the forms resulting from the above declarations will contain 225 225 exactly the same fields. 226 226 227 ``exclude_add`` 228 ~~~~~~~~~~~~~~~ 229 230 Disable the related field "add another" popup for any field listed. 231 232 For example:: 233 234 class BookAdmin(admin.ModelAdmin): 235 exclude_add = ['author'] 236 237 In the admin form for the Book model, the author field will appear without the popup link 238 next to it. 239 227 240 ``filter_horizontal`` 228 241 ~~~~~~~~~~~~~~~~~~~~~ 229 242