Ticket #9071: add-r9751.diff

File add-r9751.diff, 5.3 KB (added by jackymac, 16 years ago)

updated patch to so it applies cleanly to SVN-9751, added docs

  • django/contrib/admin/options.py

     
    3939    filter_horizontal = ()
    4040    radio_fields = {}
    4141    prepopulated_fields = {}
     42    exclude_add = []
    4243   
    4344    def formfield_for_dbfield(self, db_field, **kwargs):
    4445        """
     
    138139                # formfield can be None if it came from a OneToOneField with
    139140                # parent_link=True
    140141                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)
    142144            return formfield
    143145       
    144146        # For any other type of field, just call its formfield() method.
  • django/contrib/admin/widgets.py

     
    196196    This class is a wrapper to a given widget to add the add icon for the
    197197    admin interface.
    198198    """
    199     def __init__(self, widget, rel, admin_site):
     199    def __init__(self, widget, rel, admin_site, add=True):
    200200        self.is_hidden = widget.is_hidden
    201201        self.needs_multipart_form = widget.needs_multipart_form
    202202        self.attrs = widget.attrs
    203203        self.choices = widget.choices
    204204        self.widget = widget
    205205        self.rel = rel
     206        self.add = add
    206207        # so we can check if the related object is registered with this AdminSite
    207208        self.admin_site = admin_site
    208209
     
    222223        related_url = '../../../%s/%s/' % (rel_to._meta.app_label, rel_to._meta.object_name.lower())
    223224        self.widget.choices = self.choices
    224225        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:
    226228            # TODO: "id_" is hard-coded here. This should instead use the correct
    227229            # API to determine the ID dynamically.
    228230            output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
  • tests/regressiontests/admin_widgets/models.py

     
    4848>>> from django.contrib.admin.widgets import FilteredSelectMultiple, AdminSplitDateTime
    4949>>> from django.contrib.admin.widgets import AdminFileWidget, ForeignKeyRawIdWidget, ManyToManyRawIdWidget
    5050>>> from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
     51>>> from django.contrib.admin import site
     52>>> from django.forms.widgets import Select as SelectWidget
    5153
    5254Calling conditional_escape on the output of widget.render will simulate what
    5355happens in the template. This is easier than setting up a template and context
     
    116118>>> child_of_hidden = Inventory.objects.create(barcode=94, name='Child of hidden', parent=hidden)
    117119>>> print w.render('test', child_of_hidden.parent_id, attrs={})
    118120<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>&nbsp;<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
    119135""" % {
    120136    'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX,
    121137    'STORAGE_URL': default_storage.url(''),
  • docs/ref/contrib/admin.txt

     
    224224``birth_date``, the forms resulting from the above declarations will contain
    225225exactly the same fields.
    226226
     227``exclude_add``
     228~~~~~~~~~~~~~~~
     229
     230Disable the related field "add another" popup for any field listed.
     231
     232For example::
     233   
     234    class BookAdmin(admin.ModelAdmin):
     235        exclude_add = ['author']
     236
     237In the admin form for the Book model, the author field will appear without the popup link
     238next to it.
     239
    227240``filter_horizontal``
    228241~~~~~~~~~~~~~~~~~~~~~
    229242
Back to Top