Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Addresses #17496 and adds a no_index option to ModelAdmin #107

Closed
wants to merge 1 commit into from

2 participants

Samuel Sutch Julien Phalip
Samuel Sutch

Adds, documents and tests a fix for #17496 which adds a new option
to ModelAdmin called no_index, which when set to True, will exclude
that Model from being displayed in the AdminSite's indexes (both
the primary index and the app-level index).

For convenience: https://code.djangoproject.com/ticket/17498

Addresses #17496 and adds a no_index option to ModelAdmin
Adds, documents and tests a fix for #17496 which adds a new option
to ModelAdmin called no_index, which when set to True, will exclude
that Model from being displayed in the AdminSite's indexes (both
the primary index and the app-level index).
c5a5f06
Samuel Sutch

@jphalip last patch from the sprint today. Hope to see you next time. Let me know if this needs anything more.

Julien Phalip
Owner

Thanks a lot for your work, however I'm going to reject this pull request for the reasons explained in https://code.djangoproject.com/ticket/17498#comment:8

Julien Phalip jphalip closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 03, 2012
Addresses #17496 and adds a no_index option to ModelAdmin
Adds, documents and tests a fix for #17496 which adds a new option
to ModelAdmin called no_index, which when set to True, will exclude
that Model from being displayed in the AdminSite's indexes (both
the primary index and the app-level index).
c5a5f06
This page is out of date. Refresh to see the latest.
1  django/contrib/admin/options.py
@@ -318,6 +318,7 @@ class ModelAdmin(BaseModelAdmin):
318 318
     search_fields = ()
319 319
     date_hierarchy = None
320 320
     save_as = False
  321
+    no_index = False
321 322
     save_on_top = False
322 323
     paginator = Paginator
323 324
     inlines = []
8  django/contrib/admin/sites.py
@@ -334,6 +334,10 @@ def index(self, request, extra_context=None):
334 334
         app_dict = {}
335 335
         user = request.user
336 336
         for model, model_admin in self._registry.items():
  337
+            # skip models that do not wish to be shown on the index page
  338
+            if model_admin.no_index:
  339
+                continue
  340
+
337 341
             app_label = model._meta.app_label
338 342
             has_module_perms = user.has_module_perms(app_label)
339 343
 
@@ -391,6 +395,10 @@ def app_index(self, request, app_label, extra_context=None):
391 395
         app_dict = {}
392 396
         for model, model_admin in self._registry.items():
393 397
             if app_label == model._meta.app_label:
  398
+                # ignore this model from the app's index if no_index
  399
+                if model_admin.no_index:
  400
+                    continue
  401
+
394 402
                 if has_module_perms:
395 403
                     perms = model_admin.get_model_perms(request)
396 404
 
11  docs/ref/contrib/admin/index.txt
@@ -740,6 +740,17 @@ subclass::
740 740
     regardless of this setting if one of the ``list_display`` fields is a
741 741
     ``ForeignKey``.
742 742
 
  743
+.. attribute:: ModelAdmin.no_index
  744
+
  745
+    .. versionadded:: 1.5
  746
+
  747
+    Set `no_index` to True to hide this Model from the admin index and the 
  748
+    module's index. This is useful if you want the to be able to add the model 
  749
+    via the ForeignKey's inline editing capabilities but do not wish to see it 
  750
+    in the admin indexes.
  751
+
  752
+    Defaults to True.
  753
+
743 754
 .. attribute:: ModelAdmin.ordering
744 755
 
745 756
     Set ``ordering`` to specify how lists of objects should be ordered in the
10  tests/regressiontests/admin_views/admin.py
@@ -27,7 +27,7 @@
27 27
     Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
28 28
     AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
29 29
     AdminOrderedCallable, Report, Color2, UnorderedObject, MainPrepopulated,
30  
-    RelatedPrepopulated)
  30
+    RelatedPrepopulated, Stuff, StuffBox)
31 31
 
32 32
 
33 33
 def callable_year(dt_value):
@@ -569,6 +569,12 @@ class UnorderedObjectAdmin(admin.ModelAdmin):
569 569
     list_per_page = 2
570 570
 
571 571
 
  572
+class StuffAdmin(admin.ModelAdmin):
  573
+    """
  574
+    A ModelAdmin that is hidden from the app index
  575
+    """
  576
+    no_index = True
  577
+
572 578
 
573 579
 site = admin.AdminSite(name="admin")
574 580
 site.register(Article, ArticleAdmin)
@@ -616,6 +622,8 @@ class UnorderedObjectAdmin(admin.ModelAdmin):
616 622
 site.register(Report, ReportAdmin)
617 623
 site.register(MainPrepopulated, MainPrepopulatedAdmin)
618 624
 site.register(UnorderedObject, UnorderedObjectAdmin)
  625
+site.register(Stuff, StuffAdmin)
  626
+site.register(StuffBox)
619 627
 
620 628
 # We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
621 629
 # That way we cover all four cases:
16  tests/regressiontests/admin_views/models.py
@@ -606,3 +606,19 @@ class UnorderedObject(models.Model):
606 606
     """
607 607
     name = models.CharField(max_length=255)
608 608
     bool = models.BooleanField(default=True)
  609
+
  610
+
  611
+class Stuff(models.Model):
  612
+    """
  613
+    Model to test no_index=True/False setting no_index=True on this model's
  614
+    registered ModelAdmin will exclude it from the index but still allow it to
  615
+    be added via the "+" button when referenced via ForeignKey
  616
+    """
  617
+    name = models.IntegerField(default=1)
  618
+
  619
+
  620
+class StuffBox(models.Model):
  621
+    """
  622
+    The "+" button should show up next to `stuff` when adding a StuffBox
  623
+    """
  624
+    stuff = models.ForeignKey(Stuff)
32  tests/regressiontests/admin_views/tests.py
@@ -2840,6 +2840,38 @@ def test_ordered_inline(self):
2840 2840
 
2841 2841
 
2842 2842
 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
  2843
+class NoIndexTests(TestCase):
  2844
+    urls = "regressiontests.admin_views.urls"
  2845
+    fixtures = ['admin-views-users.xml']
  2846
+
  2847
+    def setUp(self):
  2848
+        self.client.login(username='super', password='secret')
  2849
+
  2850
+    def tearDown(self):
  2851
+        self.client.logout()
  2852
+
  2853
+    def test_admin_index_no_index(self):
  2854
+        "no_index=True on model admin should exclude itself from the index"
  2855
+        response = self.client.get('/test_admin/admin/')
  2856
+        # a link to the stuff admin should not be present
  2857
+        self.assertContains(response, '<a href="/test_admin/admin/admin_views/stuffbox/">Stuff boxs</a>', html=True)
  2858
+        self.assertNotContains(response, '<a href="/test_admin/admin/admin_views/stuff/">Stuffs</a>', html=True)
  2859
+
  2860
+    def test_admin_app_index_no_index(self):
  2861
+        "no_index=True on model admin should exclude itself from its app index"
  2862
+        response = self.client.get('/test_admin/admin/admin_views/')
  2863
+        # a link to the stuff admin should not present
  2864
+        self.assertContains(response, '<a href="/test_admin/admin/admin_views/stuffbox/">Stuff boxs</a>', html=True)
  2865
+        self.assertNotContains(response, '<a href="/test_admin/admin/admin_views/stuff/">Stuffs</a>', html=True)
  2866
+
  2867
+    def test_admin_no_index_inline_available(self):
  2868
+        "no_index=True should allow inline editing capability"
  2869
+        response = self.client.get('/test_admin/admin/admin_views/stuffbox/add/')
  2870
+        # an add button should be next to the `stuff` field for the StuffBox form
  2871
+        self.assertContains(response, 'id="add_id_stuff"')
  2872
+
  2873
+
  2874
+@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
2843 2875
 class NeverCacheTests(TestCase):
2844 2876
     urls = "regressiontests.admin_views.urls"
2845 2877
     fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.