Python django.contrib.admin.options.ModelAdmin() Examples

The following are 30 code examples of django.contrib.admin.options.ModelAdmin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.contrib.admin.options , or try the search function .
Example #1
Source File: admin.py    From django-tabbed-admin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_inline_instances(self, request, obj=None):
        """
        Overwrites BaseModelAdmin fieldsets to add fieldsets passed by the
        tabs.
        If the tabs attribute is not set, use the default ModelAdmin method.
        """
        if self.tabs is not None:
            self.inlines = ()
        tabs_inlines = self.get_formatted_tabs(request, obj)['inlines']
        self.inlines = self.add_tabbed_item(tabs_inlines, self.inlines)

        try:
            # django >=1.7
            return super(TabbedModelAdmin, self)\
                .get_inline_instances(request, obj)
        except TypeError:
            return super(TabbedModelAdmin, self).get_inline_instances(request) 
Example #2
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_list_display_link_checked_for_list_tuple_if_get_list_display_overridden(self):
        """
        list_display_links is checked for list/tuple/None even if
        get_list_display() is overridden.
        """
        class TestModelAdmin(ModelAdmin):
            list_display_links = 'non-list/tuple'

            def get_list_display(self, request):
                pass

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_display_links' must be a list, a tuple, or None.",
            'admin.E110'
        ) 
Example #3
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_not_iterable(self):
        class TestModelAdmin(ModelAdmin):
            ordering = 10

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'ordering' must be a list or tuple.",
            'admin.E031'
        )

        class TestModelAdmin(ModelAdmin):
            ordering = ('non_existent_field',)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'ordering[0]' refers to 'non_existent_field', "
            "which is not an attribute of 'modeladmin.ValidationTestModel'.",
            'admin.E033'
        ) 
Example #4
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_not_filter_again_again(self):
        class AwesomeFilter(SimpleListFilter):
            def get_title(self):
                return 'awesomeness'

            def get_choices(self, request):
                return (('bit', 'A bit awesome'), ('very', 'Very awesome'))

            def get_queryset(self, cl, qs):
                return qs

        class TestModelAdmin(ModelAdmin):
            list_filter = (('is_active', AwesomeFilter),)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_filter[0][1]' must inherit from 'FieldListFilter'.",
            'admin.E115'
        ) 
Example #5
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_field(self):
        class TestModelAdmin(ModelAdmin):
            date_hierarchy = 'non_existent_field'

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'date_hierarchy' refers to 'non_existent_field', "
            "which does not refer to a Field.",
            'admin.E127'
        ) 
Example #6
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class ValidationTestInline(TabularInline):
            model = ValidationTestInlineModel
            extra = 2

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #7
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_invalid_field_type(self):
        class TestModelAdmin(ModelAdmin):
            date_hierarchy = 'name'

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
            'admin.E128'
        ) 
Example #8
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class TestModelAdmin(ModelAdmin):
            date_hierarchy = 'pub_date'

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #9
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_related_invalid_field_type(self):
        class TestModelAdmin(ModelAdmin):
            date_hierarchy = 'band__name'

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'date_hierarchy' must be a DateField or DateTimeField.",
            'admin.E128'
        ) 
Example #10
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_field(self):
        class ValidationTestInline(TabularInline):
            model = ValidationTestInlineModel
            fk_name = 'non_existent_field'

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "'modeladmin.ValidationTestInlineModel' has no field named 'non_existent_field'.",
            'admin.E202',
            invalid_obj=ValidationTestInline
        ) 
Example #11
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_iterable(self):
        class TestModelAdmin(ModelAdmin):
            inlines = 10

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'inlines' must be a list or tuple.",
            'admin.E103'
        ) 
Example #12
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_model_admin(self):
        class ValidationTestInline:
            pass

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsInvalidRegexp(
            TestModelAdmin, ValidationTestModel,
            r"'.*\.ValidationTestInline' must inherit from 'InlineModelAdmin'\.",
            'admin.E104'
        ) 
Example #13
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_model_field(self):
        class ValidationTestInline(TabularInline):
            pass

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsInvalidRegexp(
            TestModelAdmin, ValidationTestModel,
            r"'.*\.ValidationTestInline' must have a 'model' attribute\.",
            'admin.E105'
        ) 
Example #14
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class ValidationTestInline(TabularInline):
            model = ValidationTestInlineModel

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #15
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_integer(self):
        class ValidationTestInline(TabularInline):
            model = ValidationTestInlineModel
            extra = 'hello'

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'extra' must be an integer.",
            'admin.E203',
            invalid_obj=ValidationTestInline
        ) 
Example #16
Source File: admin.py    From django-tabbed-admin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_fieldsets(self, request, obj=None):
        """
        Overwrites BaseModelAdmin fieldsets to add fieldsets passed by the
        tabs.
        If the tabs attribute is not set, use the default ModelAdmin method.
        """
        tabs_fieldsets = self.get_formatted_tabs(request, obj)['fieldsets']
        if self.tabs is not None:
            self.fieldsets = ()
        self.fieldsets = self.add_tabbed_item(tabs_fieldsets, self.fieldsets)
        return super(TabbedModelAdmin, self).get_fieldsets(request, obj) 
Example #17
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_field(self):
        class TestModelAdmin(ModelAdmin):
            list_display_links = ('non_existent_field',)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel, (
                "The value of 'list_display_links[0]' refers to "
                "'non_existent_field', which is not defined in 'list_display'."
            ), 'admin.E111'
        ) 
Example #18
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_iterable(self):
        class TestModelAdmin(ModelAdmin):
            search_fields = 10

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'search_fields' must be a list or tuple.",
            'admin.E126'
        ) 
Example #19
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class TestModelAdmin(ModelAdmin):
            list_max_show_all = 200

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #20
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class TestModelAdmin(ModelAdmin):
            list_per_page = 100

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #21
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_integer(self):
        class TestModelAdmin(ModelAdmin):
            list_per_page = 'hello'

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_per_page' must be an integer.",
            'admin.E118'
        ) 
Example #22
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class AwesomeFilter(SimpleListFilter):
            def get_title(self):
                return 'awesomeness'

            def get_choices(self, request):
                return (('bit', 'A bit awesome'), ('very', 'Very awesome'))

            def get_queryset(self, cl, qs):
                return qs

        class TestModelAdmin(ModelAdmin):
            list_filter = ('is_active', AwesomeFilter, ('is_active', BooleanFieldListFilter), 'no')

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #23
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_associated_with_field_name(self):
        class TestModelAdmin(ModelAdmin):
            list_filter = (BooleanFieldListFilter,)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_filter[0]' must not inherit from 'FieldListFilter'.",
            'admin.E114'
        ) 
Example #24
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_not_filter(self):
        class RandomClass:
            pass

        class TestModelAdmin(ModelAdmin):
            list_filter = (RandomClass,)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_filter[0]' must inherit from 'ListFilter'.",
            'admin.E113'
        ) 
Example #25
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_field(self):
        class TestModelAdmin(ModelAdmin):
            list_filter = ('non_existent_field',)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_filter[0]' refers to 'non_existent_field', "
            "which does not refer to a Field.",
            'admin.E116'
        ) 
Example #26
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_list_filter_validation(self):
        class TestModelAdmin(ModelAdmin):
            list_filter = 10

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_filter' must be a list or tuple.",
            'admin.E112'
        ) 
Example #27
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_list_display_links_check_skipped_if_get_list_display_overridden(self):
        """
        list_display_links check is skipped if get_list_display() is overridden.
        """
        class TestModelAdmin(ModelAdmin):
            list_display_links = ['name', 'subtitle']

            def get_list_display(self, request):
                pass

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #28
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        def a_callable(obj):
            pass

        class TestModelAdmin(ModelAdmin):
            def a_method(self, obj):
                pass
            list_display = ('name', 'decade_published_in', 'a_method', a_callable)
            list_display_links = ('name', 'decade_published_in', 'a_method', a_callable)

        self.assertIsValid(TestModelAdmin, ValidationTestModel) 
Example #29
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_missing_in_list_display(self):
        class TestModelAdmin(ModelAdmin):
            list_display_links = ('name',)

        self.assertIsInvalid(
            TestModelAdmin, ValidationTestModel,
            "The value of 'list_display_links[0]' refers to 'name', which is not defined in 'list_display'.",
            'admin.E111'
        ) 
Example #30
Source File: test_checks.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_valid_case(self):
        class ValidationTestInline(TabularInline):
            model = ValidationTestInlineModel
            max_num = 2

        class TestModelAdmin(ModelAdmin):
            inlines = [ValidationTestInline]

        self.assertIsValid(TestModelAdmin, ValidationTestModel)