Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Django dynamics ChoiceFields choices from APIs raises ValueError

I found the answer on this How to set choices in dynamic with Django choicefield?
maersu post has high vote on my question.

The output from get_branch() is ["first_branch", "second_branch"]

django==1.11
python 3.6.0

forms.py

class FactoryPOSForm(forms.ModelForm):
    branch_name = forms.ChoiceField(choices=['aa', 'bb'])

    class Meta:
        model = FactoryPOS
        fields = [
            'branch_name',
            'dealer_name',
            'shop_name',
            'factory',
        ]

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['branch_name'].choices = get_branches()

Error:

File "/Users/el/.pyenv/versions/eneos-factory/lib/python3.6/site-packages/django/forms/boundfield.py", line 250, in build_widget_attrs
    if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
  File "/Users/el/.pyenv/versions/eneos-factory/lib/python3.6/site-packages/django/forms/widgets.py", line 690, in use_required_attribute
    return use_required_attribute and first_choice is not None and self._choice_has_empty_value(first_choice)
  File "/Users/el/.pyenv/versions/eneos-factory/lib/python3.6/site-packages/django/forms/widgets.py", line 673, in _choice_has_empty_value
    value, _ = choice
ValueError: too many values to unpack (expected 2)
[22/Apr/2017 16:47:04] ERROR [django.server:124] "GET /admin/factories_pos/factorypos/add/ HTTP/1.1" 500 445135

Where am I miss something?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

From doc, Choices should be

An iterable (e.g., a list or tuple) consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) ...]) to use as choices for this field. If this is given, the default form widget will be a select box with these choices instead of the standard text field

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda