From 111caf86a555c3e01389fd3b0fb2d3668086b31b Mon Sep 17 00:00:00 2001 From: Alejandro Giacometti Date: Fri, 19 Dec 2014 15:03:30 +0000 Subject: [PATCH 1/2] FieldPanel to accept custom widgets FieldPanel accepts a widget argument which will override the default widget for this field (torchbox/wagtail#848) --- wagtail/wagtailadmin/edit_handlers.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/wagtail/wagtailadmin/edit_handlers.py b/wagtail/wagtailadmin/edit_handlers.py index e2ce14c35c..7e457be2fd 100644 --- a/wagtail/wagtailadmin/edit_handlers.py +++ b/wagtail/wagtailadmin/edit_handlers.py @@ -351,6 +351,15 @@ def MultiFieldPanel(children, heading="", classname=""): class BaseFieldPanel(EditHandler): + + @classmethod + def widget_overrides(cls): + """check if a specific widget has been defined for this field""" + if hasattr(cls, 'widget'): + return {cls.field_name: cls.widget} + else: + return {} + def __init__(self, instance=None, form=None): super(BaseFieldPanel, self).__init__(instance=instance, form=form) self.bound_field = self.form[self.field_name] @@ -397,11 +406,16 @@ class BaseFieldPanel(EditHandler): return [self.field_name] -def FieldPanel(field_name, classname=""): - return type(str('_FieldPanel'), (BaseFieldPanel,), { +def FieldPanel(field_name, classname="", widget=None): + base = { 'field_name': field_name, 'classname': classname, - }) + } + + if widget: + base['widget'] = widget + + return type(str('_FieldPanel'), (BaseFieldPanel,), base) class BaseRichTextFieldPanel(BaseFieldPanel): From d73e618e83b85168f64defd1bfabb940c5b1adab Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 14 Jan 2015 22:06:38 +0000 Subject: [PATCH 2/2] docs and release note for #880 --- CHANGELOG.txt | 1 + docs/core_components/pages/editing_api.rst | 6 ++++-- docs/releases/0.9.rst | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f8378da498..22d894fe81 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -9,6 +9,7 @@ Changelog * Added thousands separator for counters on dashboard * Added contextual links to admin notification messages * When copying pages, it is now possible to specify a place to copy to (Timo Rieber) + * FieldPanel now accepts an optional 'widget' parameter to override the field's default form widget (Alejandro Giacometti) 0.8.5 (xx.xx.20xx) diff --git a/docs/core_components/pages/editing_api.rst b/docs/core_components/pages/editing_api.rst index 94bbb8e88e..2087f8c879 100644 --- a/docs/core_components/pages/editing_api.rst +++ b/docs/core_components/pages/editing_api.rst @@ -23,8 +23,10 @@ A "panel" is the basic editing block in Wagtail. Wagtail will automatically pick There are four basic types of panels: - ``FieldPanel( field_name, classname=None )`` - This is the panel used for basic Django field types. ``field_name`` is the name of the class property used in your model definition. ``classname`` is a string of optional CSS classes given to the panel which are used in formatting and scripted interactivity. By default, panels are formatted as inset fields. The CSS class ``full`` can be used to format the panel so it covers the full width of the Wagtail page editor. The CSS class ``title`` can be used to mark a field as the source for auto-generated slug strings. + ``FieldPanel( field_name, classname=None, widget=None )`` + This is the panel used for basic Django field types. ``field_name`` is the name of the class property used in your model definition. ``classname`` is a string of optional CSS classes given to the panel which are used in formatting and scripted interactivity. By default, panels are formatted as inset fields. The CSS class ``full`` can be used to format the panel so it covers the full width of the Wagtail page editor. The CSS class ``title`` can be used to mark a field as the source for auto-generated slug strings. The optional ``widget`` parameter allows you to specify a `django form widget`_ to use instead of the default widget for this field type. + +.. _django form widget: https://docs.djangoproject.com/en/dev/ref/forms/widgets/ ``MultiFieldPanel( children, heading="", classname=None )`` This panel condenses several ``FieldPanel`` s or choosers, from a list or tuple, under a single ``heading`` string. diff --git a/docs/releases/0.9.rst b/docs/releases/0.9.rst index f70778bd30..c3b0f5c1df 100644 --- a/docs/releases/0.9.rst +++ b/docs/releases/0.9.rst @@ -18,6 +18,7 @@ Minor features * Added thousands separator for counters on dashboard * Added contextual links to admin notification messages * When copying pages, it is now possible to specify a place to copy to + * ``FieldPanel`` now accepts an optional ``widget`` parameter to override the field's default form widget Bug fixes