简体   繁体   中英

Setting year range for Mezzanine inline date editor

I'm having trouble with configuring the Mezzanine in-line editing for datetime fields.

I have a field in the page something like this (as in http://mezzanine.jupo.org/docs/inline-editing.html )

{% editable page.project.start %}
  {{ page.project.start|date:'Y M'}}
{% endeditable %}

However, the widget is showing only 9 future years (as https://docs.djangoproject.com/en/2.1/ref/forms/widgets/#selectdatewidget ) specifies, so it's impossible to enter any dates in the past.

I'd need to set the years attribute for that widget, however, I don't have any direct access to it, as it's generated automagically by Mezzanine. How can I set the arguments to Django widgets through the Mezzanine in-line editing code?

I don't think there is an easy way to do this, but here is one approach that might help.

Mezzanine looks for a FORMS_EXTRA_WIDGETS setting which you can use to define custom widgets. How this works is best understood from the code .

The widgets defined here are what is used to render the inline editor forms, so you should be able to supply your own. There is no way to supply initialisation arguments to the widget, so you would need to subclass the default SelectDateWidget and override it's __init__ method, something like:

class CustomSelectDateWidget(SelectDateWidget):

    def __init__(self, attrs=None, years=None, months=None, empty_label=None):
        years = [2017, 2018, 2019]   # Provide your list of years here
        super().__init__(attrs, years, months, empty_label)

And then pass this custom widget to the setting:

from mezzanine.forms.fields import DATE
FORMS_EXTRA_WIDGETS = [
    (DATE, 'dotted.path.to.CustomSelectDateWidget')
]

Obviously this will replace the widget use for all date fields on all forms rendered by Mezzanine. I can't see any way to do it specifically for one field without rewriting a lot of the inline editor logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM