Ticket #11058: 11058_list_display_links_validation.diff

File 11058_list_display_links_validation.diff, 4.8 KB (added by julien, 4 years ago)
  • django/contrib/admin/validation.py

    diff --git a/django/contrib/admin/validation.py b/django/contrib/admin/validation.py
    index 027db63..ba35256 100644
    a b def validate(cls, model): 
    4646    if hasattr(cls, 'list_display_links'):
    4747        check_isseq(cls, 'list_display_links', cls.list_display_links)
    4848        for idx, field in enumerate(cls.list_display_links):
    49             fetch_attr(cls, model, opts, 'list_display_links[%d]' % idx, field)
    5049            if field not in cls.list_display:
    51                 raise ImproperlyConfigured("'%s.list_display_links[%d]'"
     50                raise ImproperlyConfigured("'%s.list_display_links[%d]' "
    5251                        "refers to '%s' which is not defined in 'list_display'."
    5352                        % (cls.__name__, idx, field))
    5453
  • docs/ref/contrib/admin/index.txt

    diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt
    index 32668e7..326cfcd 100644
    a b subclass:: 
    479479    By default, the change list page will link the first column -- the first
    480480    field specified in ``list_display`` -- to the change page for each item.
    481481    But ``list_display_links`` lets you change which columns are linked. Set
    482     ``list_display_links`` to a list or tuple of field names (in the same
     482    ``list_display_links`` to a list or tuple of fields (in the same
    483483    format as ``list_display``) to link.
    484484
    485     ``list_display_links`` can specify one or many field names. As long as the
    486     field names appear in ``list_display``, Django doesn't care how many (or
     485    ``list_display_links`` can specify one or many fields. As long as the
     486    fields appear in ``list_display``, Django doesn't care how many (or
    487487    how few) fields are linked. The only requirement is: If you want to use
    488488    ``list_display_links``, you must define ``list_display``.
    489489
  • tests/regressiontests/modeladmin/models.py

    diff --git a/tests/regressiontests/modeladmin/models.py b/tests/regressiontests/modeladmin/models.py
    index 20dfe2c..fda49d4 100644
    a b class ValidationTestModel(models.Model): 
    3131    is_active = models.BooleanField()
    3232    pub_date = models.DateTimeField()
    3333    band = models.ForeignKey(Band)
     34   
     35    def decade_published_in(self):
     36        return self.pub_date.strftime('%Y')[:3] + "0's"
    3437
    3538class ValidationTestInlineModel(models.Model):
    3639    parent = models.ForeignKey(ValidationTestModel)
  • tests/regressiontests/modeladmin/tests.py

    diff --git a/tests/regressiontests/modeladmin/tests.py b/tests/regressiontests/modeladmin/tests.py
    index 042c1b7..13c4151 100644
    a b class ValidationTests(unittest.TestCase): 
    771771            ValidationTestModel,
    772772        )
    773773
     774        def a_callable(obj):
     775            pass
     776       
    774777        class ValidationTestModelAdmin(ModelAdmin):
    775             list_display = ('name',)
     778            def a_method(self, obj):
     779                pass
     780            list_display = ('name', 'decade_published_in', 'a_method', a_callable)
    776781
    777782        validate(ValidationTestModelAdmin, ValidationTestModel)
    778 
     783       
    779784    def test_list_display_links_validation(self):
    780785
    781786        class ValidationTestModelAdmin(ModelAdmin):
    class ValidationTests(unittest.TestCase): 
    794799
    795800        self.assertRaisesRegexp(
    796801            ImproperlyConfigured,
    797             "'ValidationTestModelAdmin.list_display_links\[0\]' refers to 'non_existent_field' that is neither a field, method or property of model 'ValidationTestModel'.",
     802            "'ValidationTestModelAdmin.list_display_links\[0\]' refers to 'non_existent_field' which is not defined in 'list_display'.",
    798803            validate,
    799804            ValidationTestModelAdmin,
    800805            ValidationTestModel,
    class ValidationTests(unittest.TestCase): 
    805810
    806811        self.assertRaisesRegexp(
    807812            ImproperlyConfigured,
    808             "'ValidationTestModelAdmin.list_display_links\[0\]'refers to 'name' which is not defined in 'list_display'.",
     813            "'ValidationTestModelAdmin.list_display_links\[0\]' refers to 'name' which is not defined in 'list_display'.",
    809814            validate,
    810815            ValidationTestModelAdmin,
    811816            ValidationTestModel,
    812817        )
    813818
     819        def a_callable(obj):
     820            pass
     821       
    814822        class ValidationTestModelAdmin(ModelAdmin):
    815             list_display = ('name',)
    816             list_display_links = ('name',)
    817 
     823            def a_method(self, obj):
     824                pass
     825            list_display = ('name', 'decade_published_in', 'a_method', a_callable)
     826            list_display_links = ('name', 'decade_published_in', 'a_method', a_callable)
     827           
    818828        validate(ValidationTestModelAdmin, ValidationTestModel)
    819829
    820830    def test_list_filter_validation(self):
Back to Top