choicefield

How to display choicefield select widget on template?

孤人 提交于 2019-12-12 04:09:42
问题 This is my view: CHOICES = (('10','10'), ('20','20'),('30','30'), ('50','50')) class Droplist (forms.Form): number = forms.ChoiceField(choices = CHOICES) def page_objects(request): if request.method == 'POST': # If the form has been submitted... form = Droplist(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass pass #pages = form.cleaned_data['value'] #return AutoPaginateNode(paginate_by=pages) # Redirect after POST else: form = Droplist() # An

Python - Django - Form choicefield and cleaned_data

帅比萌擦擦* 提交于 2019-12-11 23:24:17
问题 I'm having an issue with how cleaned_data seems to work. I want to store the cleaned_data in a session so I can repopulate the form object later. The problem is my choice fields seems to store the display name of the dropdown not the actual value. For example: <select name="dropdown_element"> <option value="1">Red</option> <option value="2">Blue</option> </select> If I selected Red and used form.cleaned_data['dropdown_element'] I will get the display name Red and not the value of 1. Is there

Dynamically populating choicefield in Django

元气小坏坏 提交于 2019-12-11 16:16:37
问题 I can't initialize the Choicefield form inside the views.py . I tried passing the option variable in the __init__ function but I got an error: __init__() takes at least 2 arguments (1 given)` coming from the `form = super(SomeeWizard, self).get_form(step, data, files) forms.py class SomeForm(forms.Form): def __init__(self, choice, *args, **kwargs): super(SomeForm, self).__init__(*args, **kwargs) self.fields['choices'] = forms.ChoiceField(choices=[ (o.id, str(o)) for o in choice]) views.py

Dynamic select options not validating Django

。_饼干妹妹 提交于 2019-12-11 09:46:42
问题 I have a model Foo. I have a modelform FooForm. In FooForm I am adding a dynamic field "too". This is a select field. FooForm(modelform) too = forms.ChoiceField(widget = forms.Select()) class Meta: model = Foo In the template I am adding options dynamically to this "too field" $('.too').append('<option value='timepass'>'+timepass'</option>'); In the view these values are not validating, as there are no choices available to it for comparing. How do I validate and extract this field ? Thanks in

django-filter: Using ChoiceFilter with choices dependent on request

坚强是说给别人听的谎言 提交于 2019-12-11 04:26:18
问题 I am using django-filter and need to add a ChoiceFilter with choices dependent on the request that I receive. I am reading the docs for ChoiceFilter but it says: This filter matches values in its choices argument. The choices must be explicitly passed when the filter is declared on the FilterSet . So is there any way to get request-dependent choices in the ChoiceFilter ? I haven't actually written the code but the following is what I want - class F(FilterSet): status = ChoiceFilter(choices=?)

Can not iterate a ChoiceField with Select as widget

懵懂的女人 提交于 2019-12-10 17:06:32
问题 I have issues with iterating over a ChoiceField and building my own HTML from the values and labels. When specifying "widget" parameter as Select , the field is not longer iterable. However, it works fine if I specify it as RadioSelect . The form: class MyFormCreate( Form ) : QUOTES = ( (34, "Hi, Patrick. Wait, I'M PATRICK!"), (21, "I like pie."), (76, "No, this is Patrick!"), ) patrick = ChoiceField(choices = QUOTES, widget = Select) And the template: <select name="{{form.patrick.name}}"> {%

Customizing RadioSelect

五迷三道 提交于 2019-12-09 13:30:04
问题 Hello I have a form with ChoiceField whose widget is set to RadioSelect Now to override default html output one needs to subclass RadioFieldRenderer like this: class SimpleRadioFieldRenderer(forms.widgets.RadioFieldRenderer): def render(self): """Outputs widget without <ul> or <li> tags.""" return mark_safe(u'\n'.join([u'%s' % force_unicode(w.tag()) for w in self])) All is good now except I'd like to be able to render 1 radio button at a time from template. Something like this: {{ form

Return display_name in ChoiceField

 ̄綄美尐妖づ 提交于 2019-12-07 20:33:37
问题 I'm implementing some REST API in DRF with ModelViewSet and ModelSerializer . All my APIs use the JSON format and some of my models use ChoiceField field, like that: MyModel(models.Model): KEY1 = 'Key1' KEY2 = 'Key2' ATTRIBUTE_CHOICES = ( (KEY1, 'Label 1'), (KEY2, 'Label 2')) attribute = models.CharField(max_length=4, choices=ATTRIBUTE_CHOICES, default=KEY1) My problem is that by default DRF always returns (and accept) the key of these choices for JSON messages (see here), but I would like to

How to use a choiceField declared in the model, in a form. django

二次信任 提交于 2019-12-07 13:44:17
问题 I have this in my model.py class marca(models.Model): marcas = ( ('chevrolet', 'Chevrolet'), ('mazda', 'Mazda'), ('nissan', 'Nissan'), ('toyota', 'Toyota'), ('mitsubishi', 'Mitsubishi'), ) marca = models.CharField(max_length=2, choices= marcas) def __unicode__(self): return self.marca And I need to use it in my form.py I tried this but it doesn't work. class addVehiculoForm(forms.Form): placa = forms.CharField(widget = forms.TextInput()) tipo = forms.CharField(max_length=2, widget=forms

Return display_name in ChoiceField

两盒软妹~` 提交于 2019-12-06 12:14:03
I'm implementing some REST API in DRF with ModelViewSet and ModelSerializer . All my APIs use the JSON format and some of my models use ChoiceField field, like that: MyModel(models.Model): KEY1 = 'Key1' KEY2 = 'Key2' ATTRIBUTE_CHOICES = ( (KEY1, 'Label 1'), (KEY2, 'Label 2')) attribute = models.CharField(max_length=4, choices=ATTRIBUTE_CHOICES, default=KEY1) My problem is that by default DRF always returns (and accept) the key of these choices for JSON messages (see here ), but I would like to use the label instead, because I think it's more consistent and clear to unterstand for who will use