Python源码示例:django.contrib.admin.options.ModelAdmin()
示例1
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)
示例2
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'
)
示例3
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'
)
示例4
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'
)
示例5
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)
示例6
def test_fieldset(self):
ma = ModelAdmin(Programme, self.site)
self.assertEqual(
ma.get_fields(None),
['name', 'synopsis', 'photo', 'language', 'current_season', 'category', 'slug', '_runtime', 'start_date', 'end_date'])
示例7
def test_vatin_field_admin(self):
"""Admin form is generated."""
ma = ModelAdmin(VIESModel, self.site)
try:
ma.get_form(request)
except Exception as e:
self.fail(e.message)
示例8
def test_not_iterable(self):
class TestModelAdmin(ModelAdmin):
raw_id_fields = 10
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'raw_id_fields' must be a list or tuple.",
'admin.E001'
)
示例9
def test_missing_field(self):
class TestModelAdmin(ModelAdmin):
raw_id_fields = ('non_existent_field',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'raw_id_fields[0]' refers to 'non_existent_field', "
"which is not an attribute of 'modeladmin.ValidationTestModel'.",
'admin.E002'
)
示例10
def test_invalid_field_type(self):
class TestModelAdmin(ModelAdmin):
raw_id_fields = ('name',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'raw_id_fields[0]' must be a foreign key or a "
"many-to-many field.",
'admin.E003'
)
示例11
def test_valid_case(self):
class TestModelAdmin(ModelAdmin):
raw_id_fields = ('users',)
self.assertIsValid(TestModelAdmin, ValidationTestModel)
示例12
def test_valid_case(self):
class TestModelAdmin(ModelAdmin):
fieldsets = (('General', {'fields': ('name',)}),)
self.assertIsValid(TestModelAdmin, ValidationTestModel)
示例13
def test_non_iterable_item(self):
class TestModelAdmin(ModelAdmin):
fieldsets = ({},)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fieldsets[0]' must be a list or tuple.",
'admin.E008'
)
示例14
def test_item_not_a_pair(self):
class TestModelAdmin(ModelAdmin):
fieldsets = ((),)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fieldsets[0]' must be of length 2.",
'admin.E009'
)
示例15
def test_second_element_of_item_not_a_dict(self):
class TestModelAdmin(ModelAdmin):
fieldsets = (('General', ()),)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fieldsets[0][1]' must be a dictionary.",
'admin.E010'
)
示例16
def test_missing_fields_key(self):
class TestModelAdmin(ModelAdmin):
fieldsets = (('General', {}),)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fieldsets[0][1]' must contain the key 'fields'.",
'admin.E011'
)
class TestModelAdmin(ModelAdmin):
fieldsets = (('General', {'fields': ('name',)}),)
self.assertIsValid(TestModelAdmin, ValidationTestModel)
示例17
def test_specified_both_fields_and_fieldsets(self):
class TestModelAdmin(ModelAdmin):
fieldsets = (('General', {'fields': ('name',)}),)
fields = ['name']
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"Both 'fieldsets' and 'fields' are specified.",
'admin.E005'
)
示例18
def test_duplicate_fields_in_fieldsets(self):
class TestModelAdmin(ModelAdmin):
fieldsets = [
(None, {'fields': ['name']}),
(None, {'fields': ['name']}),
]
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"There are duplicate field(s) in 'fieldsets[1][1]'.",
'admin.E012'
)
示例19
def test_fieldsets_with_custom_form_validation(self):
class BandAdmin(ModelAdmin):
fieldsets = (('Band', {'fields': ('name',)}),)
self.assertIsValid(BandAdmin, Band)
示例20
def test_duplicate_fields_in_fields(self):
class TestModelAdmin(ModelAdmin):
fields = ['name', 'name']
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fields' contains duplicate field(s).",
'admin.E006'
)
示例21
def test_inline(self):
class ValidationTestInline(TabularInline):
model = ValidationTestInlineModel
fields = 10
class TestModelAdmin(ModelAdmin):
inlines = [ValidationTestInline]
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'fields' must be a list or tuple.",
'admin.E004',
invalid_obj=ValidationTestInline
)
示例22
def test_invalid_type(self):
class FakeForm:
pass
class TestModelAdmin(ModelAdmin):
form = FakeForm
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'form' must inherit from 'BaseModelForm'.",
'admin.E016'
)
示例23
def test_valid_case(self):
class AdminBandForm(forms.ModelForm):
delete = forms.BooleanField()
class BandAdmin(ModelAdmin):
form = AdminBandForm
fieldsets = (
('Band', {
'fields': ('name', 'bio', 'sign_date', 'delete')
}),
)
self.assertIsValid(BandAdmin, Band)
示例24
def test_not_iterable(self):
class TestModelAdmin(ModelAdmin):
filter_vertical = 10
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'filter_vertical' must be a list or tuple.",
'admin.E017'
)
示例25
def test_missing_field(self):
class TestModelAdmin(ModelAdmin):
filter_vertical = ('non_existent_field',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'filter_vertical[0]' refers to 'non_existent_field', "
"which is not an attribute of 'modeladmin.ValidationTestModel'.",
'admin.E019'
)
示例26
def test_invalid_field_type(self):
class TestModelAdmin(ModelAdmin):
filter_vertical = ('name',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'filter_vertical[0]' must be a many-to-many field.",
'admin.E020'
)
示例27
def test_valid_case(self):
class TestModelAdmin(ModelAdmin):
filter_vertical = ('users',)
self.assertIsValid(TestModelAdmin, ValidationTestModel)
示例28
def test_missing_field(self):
class TestModelAdmin(ModelAdmin):
filter_horizontal = ('non_existent_field',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'filter_horizontal[0]' refers to 'non_existent_field', "
"which is not an attribute of 'modeladmin.ValidationTestModel'.",
'admin.E019'
)
示例29
def test_invalid_field_type(self):
class TestModelAdmin(ModelAdmin):
filter_horizontal = ('name',)
self.assertIsInvalid(
TestModelAdmin, ValidationTestModel,
"The value of 'filter_horizontal[0]' must be a many-to-many field.",
'admin.E020'
)
示例30
def test_valid_case(self):
class TestModelAdmin(ModelAdmin):
filter_horizontal = ('users',)
self.assertIsValid(TestModelAdmin, ValidationTestModel)