Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Form setting ChoiceField options as Form is called

I'm trying to integrate with an inventory application and allow users to add parts to inventory through a Django form. I can read from the inventory database but have to write through the API so based on what they enter into the a previous form I want to give them a list of available parts to add.

Here is my form, I set a few choices just as a default:

class PartForm(forms.Form):
    PART_CHOICES = (
    ('BT-J1KND-A', 'BT-J1KND-A'),
    ('BT-J1KND-B', 'BT-J1KND-B'),)
    part = forms.ChoiceField(choices = PART_CHOICES,required = True, label = 'Part to add to Inventory')

And here is where I set the choices

parts_list = part_sql(part)
#build choices
PART_CHOICES= [(p[0],p[0]) for p in parts_list]
form = PartForm()
form.fields['part'].choices = PART_CHOICES

It displays correctly in the template but when I go to hit save and process the form if it is not one of the default choices I put in it says it is not a valid option.

Select a valid choice. BT-J1KND-C is not one of the available choices.

How do I fix this to get it to accept the choices I set as valid options?

like image 242
enderv Avatar asked Jun 30 '13 16:06

enderv


People also ask

What is ChoiceField in Django?

ChoiceField in Django Forms is a string field, for selecting a particular choice out of a list of available choices. It is used to implement State, Countries etc. like fields for which information is already defined and user has to choose one. It is used for taking text inputs from the user.

What is form form in Django?

Django provides a Form class which is used to create HTML forms. It describes a form and how it works and appears. It is similar to the ModelForm class that creates a form by using the Model, but it does not require the Model.

What is widget in Django forms?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <! DOCTYPE html> .


1 Answers

Once the request gets processed, it seems that your user input goes into an unmodified PartForm, using the hardcoded values.

In order to get the request processed correctly, you need to have a consistent PartForm

You can achieve this smoothly by modifying the field's choices attribute at initialization.

For example:

class ExampleForm(forms.Form):
    CHOICES = (
        ('EXMPL', 'Example'),
    )
    field = forms.ChoiceField(choices=CHOICES, required=True, label='Example')

    def __init__(self, custom_choices=None, *args, **kwargs):
        super(ExampleForm, self).__init__(*args, **kwargs)
        if custom_choices:
            self.fields['field'].choices = custom_choices

And then, just remember to initialize the form correctly, with

form = ExampleForm(my_custom_choices_nested_tuple, ...)

You may also double-check where you're actually processing data (a form = PartForm(request.REQUEST) not mentioned in your copied code, I guess)

like image 169
lastmikoi Avatar answered Sep 19 '22 01:09

lastmikoi