I am trying to build a custom MultiValue field in django that consists of two widgets: RadioSelect and TextInput: if a user chooses 'Other' then they can insert the value there.
Everything works, with one weird exception: the labels for radio buttons are not shown (see picture). Values are rendered ok, but the labels are just not there. What I am doing wrong?
fields.py
from .widgets import OtherSelectorWidget
class OtherModelField(models.CharField):
def __init__(self, *args, **kwargs):
self.inner_choices = kwargs.pop('choices', None)
super().__init__(*args, **kwargs)
def formfield(self, **kwargs):
return OtherFormField(choices=self.inner_choices, **kwargs)
class OtherFormField(MultiValueField):
def __init__(self, **kwargs):
self.choices = kwargs.pop('choices')
self.widget = OtherSelectorWidget(choices=self.choices)
fields = (CharField(), CharField(),)
super().__init__(fields=fields, require_all_fields=False, **kwargs)
def compress(self, data_list):
return str(data_list)
widgets.py
from datetime import date
from django.forms import widgets
class OtherSelectorWidget(widgets.MultiWidget):
def __init__(self, choices=None, attrs=None):
self.choices = choices
_widgets = (
widgets.RadioSelect(choices=choices),
widgets.TextInput(attrs=attrs),
)
super().__init__(_widgets, attrs)
def decompress(self, value):
if value:
return [value[0], value[1]]
return [None, None, ]
def format_output(self, rendered_widgets):
return ''.join(rendered_widgets)
def value_from_datadict(self, data, files, name):
datelist = [
widget.value_from_datadict(data, files, name + '_%s' % i)
for i, widget in enumerate(self.widgets)]
radio_data = self.widgets[0].value_from_datadict(data, files, name + '_0')
text_data = self.widgets[1].value_from_datadict(data, files, name + '_1')
try:
D = [radio_data, text_data]
except ValueError:
return ''
else:
return D
it seems to be a glitch in Django. Here is a link to a ticket: https://code.djangoproject.com/ticket/29200
I dealt with it by adding wrap_label to widget's context:
class OtherSelectorWidget(widgets.MultiWidget):
def get_context(self, name, value, attrs):
con = super().get_context(name, value, attrs)
con['wrap_label'] = True
return con
Then everything is rendered properly
Related
I have a form with a required field customer_phone_number and despite passing a phone number in with the data the form isn't valid..
Submitting the form through a view works but not in the shell.
class OrderForm(forms.ModelForm):
class Meta:
model = models.Order
fields = (
'date',
'customer_phone_number',
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['customer_phone_number'].required = True
self.fields['customer_phone_number'].widget = PhoneNumberPrefixWidget()
data = {
'date': '2020-04-20',
'customer_phone_number': '+17168567800',
}
form = OrderForm(data=data)
form.is_valid()
print(form.errors) # {'customer_phone_number': ['This field is required.']}
The only thing I can think of is that I'm using a custom widget but I have no idea how to correct this error.
from phonenumber_field.phonenumber import PhoneNumber
from django.forms import Select, TextInput
from django.forms.widgets import MultiWidget
class PhonePrefixSelect(Select):
initial = '+1'
def __init__(self, attrs=None):
choices = [('+1', '+1')]
super().__init__(attrs, choices=sorted(choices, key=lambda item: item[1]))
def render(self, name, value, *args, **kwargs):
return super().render(
name, value or self.initial, *args, **kwargs)
class PhoneNumberPrefixWidget(MultiWidget):
"""
A Widget that splits phone number input into:
- a country select box for phone prefix
- an input for local phone number
"""
def __init__(self, attrs=None):
widgets = (
PhonePrefixSelect(attrs={
'class': 'form-control w-25 mr-2',
'tabindex': '-1'
}),
TextInput(attrs={
'class': 'form-control w-75'
}),
)
super().__init__(widgets, attrs)
def decompress(self, value):
if value:
if type(value) == PhoneNumber:
if value.country_code and value.national_number:
return ["+%d" % value.country_code, value.national_number]
else:
return value.split('.')
return [None, ""]
def value_from_datadict(self, data, files, name):
values = super().value_from_datadict(
data, files, name)
if all(values):
return '%s.%s' % tuple(values)
return ''
Solved by splitting customer_phone_number into two fields: customer_phone_number_0 and customer_phone_number_1
I am trying to display the init value (State) in my form, but it keep showing the "-------" value.
I know the value is not empty because it saves the states into my database. It just wont show up in my form
My form class:
class AddressUpdateForm(BaseModelForm):
state = ProvinceModelChoiceField(required=False, queryset=CountryProvincePair.objects.all())
class Meta:
model = Address
fields = ('label', 'postal', 'zip', 'city', 'country', 'state', 'custombillto')
def __init__(self, *args, **kwargs):
super(AddressUpdateForm, self).__init__(*args, **kwargs)
print("THIS IS IT", self.instance.state)
self.fields['state'].instance = self.instance.state
My custom ModelChoiceField:
class ProvinceModelChoiceField(ModelChoiceField):
"""
A model choice field that accepts custom values for province class.
Should be used together with Chosen with extended functionality.
This model will accept everything that is not in its Query initial values
"""
def label_from_instance(self, obj):
return "%s, %s" % (obj.province, obj.country)
def to_python(self, value):
if value in self.empty_values:
return None
try:
key = self.to_field_name or 'pk'
value = self.queryset.get(**{key: value})
except (ValueError, TypeError, self.queryset.model.DoesNotExist):
# If the object does not exist, does not raise an error
pass
return value
BaseModelForm:
class BaseModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(BaseModelForm, self).__init__(*args, **kwargs)
for label, field_instance in self.fields.items():
if isinstance(field_instance, ModelChoiceField) or isinstance(field_instance, ChoiceField):
field_instance.widget.attrs['class'] = 'chosen'
field_instance.help_text = ''
if isinstance(field_instance, BaseTemporalField):
field_instance.widget.attrs['class'] = 'dp'
if isinstance(field_instance, NullBooleanField):
field_instance.widget.attrs['class'] = 'switch'
if isinstance(field_instance, BooleanField):
field_instance.widget.attrs['class'] = 'switch'
I'm trying to get a placeholder to show up for a form field but I'm doing something wrong with my form fields. Any thoughts? I've got the following classes:
class EditField(forms.CharField):
def __init__(self, *args, **kwargs):
super(EditField, self).__init__(*args, **kwargs)
self.widget = forms.Textarea(attrs={'id':'editor'})
self.label = _('content')
class EditField_typeA(EditField):
def __init__(self, *args, **kwargs):
super(EditField_typeA, self).__init__(*args, **kwargs)
def clean(self, value):
if not (len(re.sub('[ ]{2,}', ' ', value)) < settings.FORM_MIN):
raise forms.ValidationError(_('content must be %s') % settings.FORM_MIN)
return value
class FinalForm(forms.Form):
foo = FooField()
bar = BarField()
text = EditField_typeA()
def __init__(self, data=None, user=None, *args, **kwargs):
super(FinalForm, self).__init__(data, *args, **kwargs)
## THIS IS THE PART THAT ISN'T WORKING
self.fields['text'].widget.attrs['placeholder'] = 'Fill this in'
if int(user.reputation) < settings.CAPTCHA_IF_REP_LESS_THAN and not (user.is_superuser or user.is_staff):
spam_fields = call_all_handlers('create_anti_spam_field')
if spam_fields:
spam_fields = dict(spam_fields)
for name, field in spam_fields.items():
self.fields[name] = field
self._anti_spam_fields = spam_fields.keys()
else:
self._anti_spam_fields = []
I'm guessing that I'm using widget wrong, or in the wrong place. Can't find the right part of the widget docs to explain what I'm doing wrong though.
I use a Django model that I register with the admin site. One of the fields of my model represents a duration. I would like to use the DateTimeField, but instead of saving the value to a datetime in the database, I would like to save it as varchar, formatted according to RFC5545 (ical) (e.g., a duration of 1 day 1 hour 1 min 1 sec would be stored as "P1DT1H1M1S"). How would I do this? Should I overwrite the DateTimeField?
You could create a custom Django field for it instead of overwriting DateTimeField.
https://docs.djangoproject.com/en/dev/howto/custom-model-fields/
Yep, just subclass model.Field. And define two methods:
Field.to_python(self, value) - will convert db value to python object.
Field.get_prep_value(self, value) - this is opposite to to_python converts object to db value.
Thanks bakkal and Pol. Below is what I came up with.
from django.db import models
from icalendar.prop import vDuration
from django.forms.widgets import MultiWidget
from django.forms import TextInput, IntegerField
from django.forms.util import flatatt
from django.forms.fields import MultiValueField
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from django.core import validators
from datetime import timedelta
def is_int(s):
try:
int(s)
return True
except ValueError:
return False
class Widget_LabelInputField(TextInput):
"""
Input widget with label
"""
input_type="numbers"
def __init__(self, labelCaption, attrs=None):
self.labelCaption = labelCaption
super(Widget_LabelInputField, self).__init__(attrs)
def _format_value(self, value):
if is_int(value):
return value
return '0'
def render(self, name, value, attrs=None):
if value is None:
value = '0'
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(self._format_value(value))
if (self.labelCaption):
typeString = self.labelCaption + ': '
else:
typeString = ''
return mark_safe(u'' + typeString + '<input%s style=\'width: 30px; margin-right: 20px\'/>' % flatatt(final_attrs))
class Widget_DurationField(MultiWidget):
"""
A Widget that splits duration input into two <input type="text"> boxes.
"""
def __init__(self, attrs=None):
widgets = (Widget_LabelInputField(labelCaption='days', attrs=attrs),
Widget_LabelInputField(labelCaption='hours', attrs=attrs),
Widget_LabelInputField(labelCaption='minutes', attrs=attrs),
Widget_LabelInputField(labelCaption='seconds', attrs=attrs)
)
super(Widget_DurationField, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
duration = vDuration.from_ical(value)
return [str(duration.days), str(duration.seconds // 3600), str(duration.seconds % 3600 // 60), str(duration.seconds % 60)]
return [None, None, None, None]
class Forms_DurationField(MultiValueField):
widget = Widget_DurationField
default_error_messages = {
'invalid_day': _(u'Enter a valid day.'),
'invalid_hour': _(u'Enter a valid hour.'),
'invalid_minute': _(u'Enter a valid minute.'),
'invalid_second': _(u'Enter a valid second.')
}
def __init__(self, *args, **kwargs):
errors = self.default_error_messages.copy()
if 'error_messages' in kwargs:
errors.update(kwargs['error_messages'])
fields = (
IntegerField(min_value=-9999, max_value=9999,
error_messages={'invalid': errors['invalid_day']},),
IntegerField(min_value=-9999, max_value=9999,
error_messages={'invalid': errors['invalid_hour']},),
IntegerField(min_value=-9999, max_value=9999,
error_messages={'invalid': errors['invalid_minute']},),
IntegerField(min_value=-9999, max_value=9999,
error_messages={'invalid': errors['invalid_second']},),
)
super(Forms_DurationField, self).__init__(fields, *args, **kwargs)
def compress(self, data_list):
if data_list:
if data_list[0] in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['invalid_day'])
if data_list[1] in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['invalid_hour'])
if data_list[2] in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['invalid_minute'])
if data_list[3] in validators.EMPTY_VALUES:
raise ValidationError(self.error_messages['invalid_second'])
return vDuration(timedelta(days=data_list[0],hours=data_list[1],minutes=data_list[2],seconds=data_list[3]))
return None
class Model_DurationField(models.Field):
description = "Duration"
def __init__(self, *args, **kwargs):
super(Model_DurationField, self).__init__(*args, **kwargs)
def db_type(self, connection):
return 'varchar(255)'
def get_internal_type(self):
return "Model_DurationField"
def to_python(self, value):
if isinstance(value, vDuration) or value is None:
return value
return vDuration.from_ical(value)
def get_prep_value(self, value):
return value.to_ical()
def formfield(self, **kwargs):
defaults = {
'form_class': Forms_DurationField,
'required': not self.blank,
'label': capfirst(self.verbose_name),
'help_text': self.help_text}
defaults.update(kwargs)
return super(Model_DurationField, self).formfield(**defaults)
I have a multivaluefield with a charfield and choicefield. I need to pass choices to the choicefield constructor, however when I try to pass it into my custom multivaluefield I get an error __init__() got an unexpected keyword argument 'choices'.
I know the rest of the code works because when I remove the choices keyword argument from __init__ and super, the multivaluefield displays correctly but without any choices.
This is how I setup my custom multivaluefield:
class InputAndChoice(object):
def __init__(self, text_val='', choice_val=''):
self.text_val=text_val
self.choice_val=choice_val
class InputAndChoiceWidget(widgets.MultiWidget):
def __init__(self, attrs=None):
widget = (widgets.TextInput(),
widgets.Select()
)
super(InputAndChoiceWidget, self).__init__(widget, attrs=attrs)
def decompress(self,value):
if value:
return [value.text_val, value.choice_val]
return [None, None]
class InputAndChoiceField(forms.MultiValueField):
widget = InputAndChoiceWidget
def __init__(self, required=True, widget=None, label=None, initial=None,
help_text=None, choices=None):
field = (
fields.CharField(),
fields.ChoiceField(choices=choices),
)
super(InputAndChoiceField, self).__init__(fields=field, widget=widget,
label=label, initial=initial, help_text=help_text, choices=choices)
And I call it like so:
input_and_choice = InputAndChoiceField(choices=[(1,'first'),(2,'second')])
So how do I pass the choices to my ChoiceField field?
Edit:
I've tried stefanw's suggestion but still no luck. I've used logging.debug to print out the contents of InputAndChoiceField at the end of the init and self.fields[1].choices contains the correct values as per above however it doesnt display any choices in the browser.
I ran into this exact same problem and solved it like this:
class InputAndChoiceWidget(widgets.MultiWidget):
def __init__(self,*args,**kwargs):
myChoices = kwargs.pop("choices")
widgets = (
widgets.TextInput(),
widgets.Select(choices=myChoices)
)
super(InputAndChoiceWidget, self).__init__(widgets,*args,**kwargs)
class InputAndChoiceField(forms.MultiValueField):
widget = InputAndChoiceWidget
def __init__(self,*args,**kwargs):
# you could also use some fn to return the choices;
# the point is, they get set dynamically
myChoices = kwargs.pop("choices",[("default","default choice")])
fields = (
fields.CharField(),
fields.ChoiceField(choices=myChoices),
)
super(InputAndChoiceField,self).__init__(fields,*args,**kwargs)
# here's where the choices get set:
self.widget = InputAndChoiceWidget(choices=myChoices)
Add a "choices" kwarg to the widget's constructor. Then explicitly call the constructor after the field is created.
ModelChoiceField is technically a ChoiceField, but it doesn't actually use any of the ChoiceField's implementations. So, here's how I use it.
class ChoiceInputMultiWidget(MultiWidget):
"""Kindly provide the choices dynamically"""
def __init__(self, attrs=None):
_widget = (
Select(attrs=attrs),
TextInput(attrs=attrs)
)
super().__init__(_widget, attrs)
class ModelChoiceInputField(MultiValueField):
widget = ChoiceInputMultiWidget
def __init__(self, *args, **kwargs):
_fields = (
ModelChoiceField(queryset=Type.objects.all()),
CharField()
)
super().__init__(_fields, *args, **kwargs)
# Use the auto-generated widget.choices by the ModelChoiceField
self.widget.widgets[0].choices = self.fields[0].widget.choices
Have a look at the source of __init__ of forms.MultiValueField:
def __init__(self, fields=(), *args, **kwargs):
super(MultiValueField, self).__init__(*args, **kwargs)
# Set 'required' to False on the individual fields, because the
# required validation will be handled by MultiValueField, not by those
# individual fields.
for f in fields:
f.required = False
self.fields = fields
So I would overwrite the __init__ probably like this:
def __init__(self, *args, **kwargs):
choices = kwargs.pop("choices",[])
super(InputAndChoiceField, self).__init__(*args, **kwargs)
self.fields = (
fields.CharField(),
fields.ChoiceField(choices=choices),
)
You might even want to do super(MultiValueField, self).__init__(*args, **kwargs) instead of super(InputAndChoiceField, self).__init__(*args, **kwargs) because you are setting the fields yourself instead of getting them via parameters.
passing the choices in the widget solved this for me
class InputAndChoiceWidget(widgets.MultiWidget):
def __init__(self, attrs=None):
choices = [('a', 1), ('b', 2)]
widget = (widgets.TextInput(),
widgets.Select(choices=choices)
)
super(InputAndChoiceWidget, self).__init__(widget, attrs=attrs)
class HTML5DateInput(DateInput):
input_type = 'date'
class CustomSelectRangeWidget(forms.MultiWidget):
def __init__(self, attrs=None, choices = ()):
widgets = (Select(attrs=attrs, choices=choices), HTML5DateInput(attrs=attrs), HTML5DateInput(attrs=attrs))
super(CustomSelectRangeWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return [value.field, value.start, value.stop]
return [None, None, None]
def format_output(self, rendered_widgets):
return '-'.join(rendered_widgets)
class CustomSelectRangeField(forms.MultiValueField):
widget = CustomSelectRangeWidget
def __init__(self, *args, **kwargs):
if kwargs.has_key('choices') :
choices = kwargs.pop('choices')
else:
choices = ()
fields = (
forms.ChoiceField(choices=choices), #field with choices,
# so that clean can be passed
forms.DateField(),
forms.DateField(),
)
super(CustomSelectRangeField, self).__init__(fields=fields, *args, **kwargs)
#initialize widget with choices.
self.widget = CustomSelectRangeWidget(choices=choices)
def compress(self, data_list):
if data_list:
#check if datalist has 3 not null values
if len([v for v in data_list if v not in [None, '']]) == 3:
out_dict = {'field':data_list[0], 'start':data_list[1], 'stop':data_list[2]}
return out_dict
return None