本文整理汇总了Python中wagtail.admin.edit_handlers.ObjectList.bind_to_instance方法的典型用法代码示例。如果您正苦于以下问题:Python ObjectList.bind_to_instance方法的具体用法?Python ObjectList.bind_to_instance怎么用?Python ObjectList.bind_to_instance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wagtail.admin.edit_handlers.ObjectList
的用法示例。
在下文中一共展示了ObjectList.bind_to_instance方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_render
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import bind_to_instance [as 别名]
def test_render(self):
"""
Check that the inline panel renders the panels set on the model
when no 'panels' parameter is passed in the InlinePanel definition
"""
speaker_object_list = ObjectList([
InlinePanel('speakers', label="Speakers", classname="classname-for-speakers")
]).bind_to_model(EventPage)
EventPageForm = speaker_object_list.get_form_class()
# SpeakerInlinePanel should instruct the form class to include a 'speakers' formset
self.assertEqual(['speakers'], list(EventPageForm.formsets.keys()))
event_page = EventPage.objects.get(slug='christmas')
form = EventPageForm(instance=event_page)
panel = speaker_object_list.bind_to_instance(instance=event_page,
form=form,
request=self.request)
result = panel.render_as_field()
self.assertIn('<li class="object classname-for-speakers">', result)
self.assertIn('<label for="id_speakers-0-first_name">Name:</label>', result)
self.assertIn('value="Father"', result)
self.assertIn('<label for="id_speakers-0-last_name">Surname:</label>', result)
self.assertIn('<label for="id_speakers-0-image">Image:</label>', result)
self.assertIn('Choose an image', result)
# rendered panel must also contain hidden fields for id, DELETE and ORDER
self.assertTagInHTML(
'<input id="id_speakers-0-id" name="speakers-0-id" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-DELETE" name="speakers-0-DELETE" type="hidden">',
result, allow_extra_attrs=True
)
self.assertTagInHTML(
'<input id="id_speakers-0-ORDER" name="speakers-0-ORDER" type="hidden">',
result, allow_extra_attrs=True
)
# rendered panel must contain maintenance form for the formset
self.assertTagInHTML(
'<input id="id_speakers-TOTAL_FORMS" name="speakers-TOTAL_FORMS" type="hidden">',
result, allow_extra_attrs=True
)
# rendered panel must include the JS initializer
self.assertIn('var panel = InlinePanel({', result)
示例2: TestObjectList
# 需要导入模块: from wagtail.admin.edit_handlers import ObjectList [as 别名]
# 或者: from wagtail.admin.edit_handlers.ObjectList import bind_to_instance [as 别名]
class TestObjectList(TestCase):
def setUp(self):
self.request = RequestFactory().get('/')
user = AnonymousUser() # technically, Anonymous users cannot access the admin
self.request.user = user
# a custom ObjectList for EventPage
self.event_page_object_list = ObjectList([
FieldPanel('title', widget=forms.Textarea),
FieldPanel('date_from'),
FieldPanel('date_to'),
InlinePanel('speakers', label="Speakers"),
], heading='Event details', classname="shiny").bind_to_model(EventPage)
def test_get_form_class(self):
EventPageForm = self.event_page_object_list.get_form_class()
form = EventPageForm()
# form must include the 'speakers' formset required by the speakers InlinePanel
self.assertIn('speakers', form.formsets)
# form must respect any overridden widgets
self.assertEqual(type(form.fields['title'].widget), forms.Textarea)
def test_render(self):
EventPageForm = self.event_page_object_list.get_form_class()
event = EventPage(title='Abergavenny sheepdog trials')
form = EventPageForm(instance=event)
object_list = self.event_page_object_list.bind_to_instance(
instance=event,
form=form,
request=self.request
)
result = object_list.render()
# result should contain ObjectList furniture
self.assertIn('<ul class="objects">', result)
# result should contain h2 headings (including labels) for children
self.assertInHTML('<h2><label for="id_date_from">Start date</label></h2>', result)
# result should include help text for children
self.assertIn('<div class="object-help help"><span class="icon-help-inverse" aria-hidden="true"></span>Not required if event is on a single day</div>', result)
# result should contain rendered content from descendants
self.assertIn('Abergavenny sheepdog trials</textarea>', result)
# this result should not include fields that are not covered by the panel definition
self.assertNotIn('signup_link', result)