Ticket #9071: disable_add_r15922.diff

File disable_add_r15922.diff, 3.5 KB (added by Dario Ocles, 13 years ago)

The last patch is obsolete. Updated the patch and adding test case.

  • docs/ref/contrib/admin/index.txt

     
    261261            ``django.utils.html.escape()`` to escape any HTML special
    262262            characters.
    263263
     264.. attribute:: ModelAdmin.exclude_add
     265
     266    Disable the related field "add another" popup for any field listed.
     267
     268    For example::
     269
     270    class BookAdmin(admin.ModelAdmin):
     271        exclude_add = ['author']
     272
     273    In the admin form for the Book model, the author field will appear without
     274    the popup link next to it.
     275
    264276.. attribute:: ModelAdmin.filter_horizontal
    265277
    266278    By default, a :class:`~django.db.models.ManyToManyField` is displayed in
  • tests/regressiontests/admin_views/tests.py

     
    116116        response = self.client.post('/test_admin/%s/admin_views/section/add/' % self.urlbit, post_data)
    117117        self.assertEqual(response.status_code, 302) # redirect somewhere
    118118
     119    def testExcludeAdd(self):
     120        """
     121        Test if we can disable "add" popup links in the admin
     122        """
     123        response = self.client.get(reverse('admin:admin_views_answer_add'))
     124        self.assertNotContains(response, """<a href="/test_admin/admin/admin_views/question/add/" class="add-another" id="add_id_question" onclick="return showAddAnotherPopup(this);"> <img src="/static/admin/img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>""")
     125
    119126    def testPopupAddPost(self):
    120127        """
    121128        Ensure http response from a popup is properly escaped.
  • tests/regressiontests/admin_views/models.py

     
    676676    def __unicode__(self):
    677677        return self.answer
    678678
     679class AnswerAdmin(admin.ModelAdmin):
     680    exclude_add = ['question']
     681
    679682class Reservation(models.Model):
    680683    start_date = models.DateTimeField()
    681684    price = models.IntegerField()
     
    817820admin.site.register(Topping)
    818821admin.site.register(Album, AlbumAdmin)
    819822admin.site.register(Question)
    820 admin.site.register(Answer)
     823admin.site.register(Answer, AnswerAdmin)
  • django/contrib/admin/options.py

     
    7070    formfield_overrides = {}
    7171    readonly_fields = ()
    7272    ordering = None
     73    exclude_add = []
    7374
    7475    def __init__(self):
    7576        overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy()
     
    113114                related_modeladmin = self.admin_site._registry.get(
    114115                                                            db_field.rel.to)
    115116                can_add_related = bool(related_modeladmin and
    116                             related_modeladmin.has_add_permission(request))
     117                            related_modeladmin.has_add_permission(request) and
     118                            db_field.name not in self.exclude_add)
    117119                formfield.widget = widgets.RelatedFieldWidgetWrapper(
    118120                            formfield.widget, db_field.rel, self.admin_site,
    119121                            can_add_related=can_add_related)
Back to Top