Ticket #9071: disable_add_r15922_2011-05-05.diff

File disable_add_r15922_2011-05-05.diff, 3.7 KB (added by Dario Ocles, 14 years ago)
  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index c603210..206399b 100644
    a b class BaseModelAdmin(object):  
    7070    formfield_overrides = {}
    7171    readonly_fields = ()
    7272    ordering = None
     73    exclude_add = []
    7374
    7475    def __init__(self):
    7576        overrides = FORMFIELD_FOR_DBFIELD_DEFAULTS.copy()
    class BaseModelAdmin(object):  
    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)
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index fb48ba8..e0f1c25 100644
    a b subclass::  
    267267            ``django.utils.html.escape()`` to escape any HTML special
    268268            characters.
    269269
     270.. attribute:: ModelAdmin.exclude_add
     271
     272    Disable the related field "add another" popup for any field listed.
     273
     274    For example::
     275
     276    class BookAdmin(admin.ModelAdmin):
     277        exclude_add = ['author']
     278
     279    In the admin form for the Book model, the author field will appear without
     280    the popup link next to it.
     281
    270282.. attribute:: ModelAdmin.filter_horizontal
    271283
    272284    By default, a :class:`~django.db.models.ManyToManyField` is displayed in
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 854fb60..4c7e8b8 100644
    a b class Answer(models.Model):  
    723723    def __unicode__(self):
    724724        return self.answer
    725725
     726class AnswerAdmin(admin.ModelAdmin):
     727    exclude_add = ['question']
     728
    726729class Reservation(models.Model):
    727730    start_date = models.DateTimeField()
    728731    price = models.IntegerField()
    admin.site.register(Pizza, PizzaAdmin)  
    864867admin.site.register(Topping)
    865868admin.site.register(Album, AlbumAdmin)
    866869admin.site.register(Question)
    867 admin.site.register(Answer)
     870admin.site.register(Answer, AnswerAdmin)
    868871admin.site.register(PrePopulatedPost, PrePopulatedPostAdmin)
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 25740bc..33dd6bf 100644
    a b class AdminViewBasicTest(TestCase):  
    119119        response = self.client.post('/test_admin/%s/admin_views/section/add/' % self.urlbit, post_data)
    120120        self.assertEqual(response.status_code, 302) # redirect somewhere
    121121
     122    def testExcludeAdd(self):
     123        """
     124        Test if we can disable "add" popup links in the admin
     125        """
     126        response = self.client.get(reverse('admin:admin_views_answer_add'))
     127        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>""")
     128
    122129    def testPopupAddPost(self):
    123130        """
    124131        Ensure http response from a popup is properly escaped.
Back to Top