本文整理汇总了Python中django.contrib.admin.ModelAdmin.get_changelist_instance方法的典型用法代码示例。如果您正苦于以下问题:Python ModelAdmin.get_changelist_instance方法的具体用法?Python ModelAdmin.get_changelist_instance怎么用?Python ModelAdmin.get_changelist_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.admin.ModelAdmin
的用法示例。
在下文中一共展示了ModelAdmin.get_changelist_instance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_choice_links
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_changelist_instance [as 别名]
def test_choice_links(self):
modeladmin = ModelAdmin(Question, site)
modeladmin.date_hierarchy = 'posted'
posted_dates = (
datetime.date(2017, 10, 1),
datetime.date(2017, 10, 1),
datetime.date(2017, 12, 15),
datetime.date(2017, 12, 15),
datetime.date(2017, 12, 31),
datetime.date(2018, 2, 1),
)
Question.objects.bulk_create(Question(question='q', posted=posted) for posted in posted_dates)
tests = (
({}, [['year=2017'], ['year=2018']]),
({'year': 2016}, []),
({'year': 2017}, [['month=10', 'year=2017'], ['month=12', 'year=2017']]),
({'year': 2017, 'month': 9}, []),
({'year': 2017, 'month': 12}, [['day=15', 'month=12', 'year=2017'], ['day=31', 'month=12', 'year=2017']]),
)
for query, expected_choices in tests:
with self.subTest(query=query):
query = {'posted__%s' % q: val for q, val in query.items()}
request = self.factory.get('/', query)
changelist = modeladmin.get_changelist_instance(request)
spec = date_hierarchy(changelist)
choices = [choice['link'] for choice in spec['choices']]
expected_choices = [
'&'.join('posted__%s' % c for c in choice) for choice in expected_choices
]
expected_choices = [('?' + choice) if choice else '' for choice in expected_choices]
self.assertEqual(choices, expected_choices)