Skip to content

Commit

Permalink
Override label_from_instance for PageChoiceField
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Jul 9, 2019
1 parent 8a0d7b7 commit f2c4ffb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions wagtailmenus/api/form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,18 @@ class PageChoiceField(forms.ModelChoiceField):
def __init__(self, *args, **kwargs):
if 'queryset' not in 'kwargs':
kwargs['queryset'] = Page.objects.none()
self.indent_choice_labels = kwargs.pop('indent_choice_labels', True)
super().__init__(*args, **kwargs)

def label_from_instance(self, obj):
if self.indent_choice_labels:
if obj.depth > 1:
indent = ''.join(' - ' for i in range(obj.depth-2))
else:
indent = ''
return '{indent}{page}'.format(indent=indent, page=obj)
return str(obj)


class SiteChoiceField(forms.ModelChoiceField):

Expand Down
1 change: 1 addition & 0 deletions wagtailmenus/api/v1/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class SectionMenuGeneratorArgumentForm(BaseMenuGeneratorArgumentForm):
section_root_page = api_form_fields.PageChoiceField(
label=_("Section root page"),
required=False,
indent_choice_labels=False,
help_text=_(
"The root page for the 'section', whose children and other "
"decendents you want show menu items for. If not supplied, the "
Expand Down

0 comments on commit f2c4ffb

Please sign in to comment.