Ticket #6202: 6202.diff

File 6202.diff, 2.5 KB (added by Erik Stein, 17 years ago)

Patch

  • django/contrib/admin/options.py

     
    147147        """
    148148        # For ManyToManyFields with a filter interface, use a special widget.
    149149        if isinstance(db_field, models.ManyToManyField) and db_field.name in (self.filter_vertical + self.filter_horizontal):
    150             kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
     150            # If related model has an admin interface, allow for add another popup
     151            rel_to = db_field.rel.to in self.admin_site._registry and db_field.rel.to or None
     152            kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical), rel_to=rel_to)
    151153            return db_field.formfield(**kwargs)
    152154
    153155        # For DateTimeFields, use a special field and widget.
  • django/contrib/admin/widgets.py

     
    1616    Note that the resulting JavaScript assumes that the SelectFilter2.js
    1717    library and its dependencies have been loaded in the HTML page.
    1818    """
    19     def __init__(self, verbose_name, is_stacked, attrs=None, choices=()):
     19    def __init__(self, verbose_name, is_stacked, attrs=None, choices=(), rel_to=None):
    2020        self.verbose_name = verbose_name
    2121        self.is_stacked = is_stacked
     22        self.rel_to = rel_to
    2223        super(FilteredSelectMultiple, self).__init__(attrs, choices)
    2324
    2425    def render(self, name, value, attrs=None, choices=()):
     
    2930        # API to determine the ID dynamically.
    3031        output.append(u'SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % \
    3132            (name, self.verbose_name.replace('"', '\\"'), int(self.is_stacked), settings.ADMIN_MEDIA_PREFIX))
     33        if self.rel_to:
     34            related_url = '../../../%s/%s/' % (self.rel_to._meta.app_label, self.rel_to._meta.object_name.lower())
     35            output.append(u'<a href="%sadd/" class="add-another" id="add_id_%s" onclick="return showAddAnotherPopup(this);"> ' % \
     36                (related_url, name))
     37            output.append(u'<img src="%simg/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>' % settings.ADMIN_MEDIA_PREFIX)
    3238        return mark_safe(u''.join(output))
    3339
    3440class AdminDateWidget(forms.TextInput):
Back to Top