django-multiwidget

django multivaluefield & multiwidget - make one optional

ⅰ亾dé卋堺 提交于 2020-01-06 03:13:08
问题 This is related to an earlier question of mine. I want to have a MultiValueField which contains a Choice and TextInput widget. If the user selects "OTHER" from the Choice, then the value of the TextInput should be saved. Otherwise, the value of the Choice should be saved. So far I have the following code: custom_choices = [("one","one"),("two","two"),("OTHER","OTHER")] class MyMultiWidget(forms.MultiWidget): def __init__(self,*args,**kwargs): widgets = ( forms.Select(choices=custom_choices),

django multivaluefield & multiwidget - compress and/or decompress not working

旧街凉风 提交于 2019-12-21 21:43:18
问题 All, I have a form with a MultiValueField that almost works. It uses a choicefield and charfield (with a corresponding Select and TextInput for the widgets):: custom_choices = [("one","one"),("two","two"),("other","other")] class MyMultiWidget(forms.MultiWidget): def __init__(self,*args,**kwargs): widgets = ( forms.Select(choices=custom_choices), forms.TextInput(), ) super(MyMultiWidget, self).__init__(widgets,*args,**kwargs) def decompress(self, value): if value: return value.split("|")

How to represent two model fields as one form field in Django?

偶尔善良 提交于 2019-12-05 13:13:03
问题 I can't seem to figure out how to handle the following situation properly in Django: I have a date range in a model, which I store as two separate fields, date_start and date_end : start_date = models.DateTimeField() end_date = models.DateTimeField() In the form for this model, I want to represent this as one field , with one label: timespan = forms.Field(widget=widgets.SelectDateRangeWidget(), label="Date Range") As it is now, I extended MultiWidget to create the SelectDateRangeWidget :

django multivaluefield & multiwidget - compress and/or decompress not working

亡梦爱人 提交于 2019-12-04 16:28:45
All, I have a form with a MultiValueField that almost works. It uses a choicefield and charfield (with a corresponding Select and TextInput for the widgets):: custom_choices = [("one","one"),("two","two"),("other","other")] class MyMultiWidget(forms.MultiWidget): def __init__(self,*args,**kwargs): widgets = ( forms.Select(choices=custom_choices), forms.TextInput(), ) super(MyMultiWidget, self).__init__(widgets,*args,**kwargs) def decompress(self, value): if value: return value.split("|") return ['', ''] class MyMultiValueField(forms.MultiValueField): def __init__(self, *args, **kwargs): fields

How do I use Django's MultiWidget?

女生的网名这么多〃 提交于 2019-11-28 02:52:48
The documentation is a bit lacking with respect to this feature. from django import forms class TwoInputWidget(forms.MultiWidget): """An example widget which concatenates two text inputs with a space""" def __init__(self, attrs=None): widgets = [forms.TextInput, forms.TextInput] I can see I need to create a "widgets" property with a list of other widgets, but after that it gets a little Sherlock Holmes. Would someone please explain to me how to use the MultiWidget widget? Interesting question and I think perhaps deserving of a little more attention in the docs. Here's an example from a

Django subclassing multiwidget - reconstructing date on post using custom multiwidget

只愿长相守 提交于 2019-11-27 17:33:00
问题 So my django book is back at university and I'm struggling to work this one out. I've subclassed django.forms.widgets.MultiWidget like so: class DateSelectorWidget(widgets.MultiWidget): def __init__(self, attrs=None, dt=None, mode=0): if dt is not None: self.datepos = dt else: self.datepos = date.today() # bits of python to create days, months, years # example below, the rest snipped for neatness. years = [(year, year) for year in year_digits] _widgets = ( widgets.Select(attrs=attrs, choices

How do I use Django's MultiWidget?

不羁的心 提交于 2019-11-26 23:51:56
问题 The documentation is a bit lacking with respect to this feature. from django import forms class TwoInputWidget(forms.MultiWidget): """An example widget which concatenates two text inputs with a space""" def __init__(self, attrs=None): widgets = [forms.TextInput, forms.TextInput] I can see I need to create a "widgets" property with a list of other widgets, but after that it gets a little Sherlock Holmes. Would someone please explain to me how to use the MultiWidget widget? 回答1: Interesting