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

78
Visualizações
Loading choices into choicefield - strange behavior

I have a django form with a choicefield, where I dynamically load some choices into the field:

class EntryForm(forms.Form):

    project = forms.ChoiceField()

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super(EntryForm, self).__init__( *args, **kwargs)

        CHOICES2=[]
        for x in Project.objects.all() :
            if user in x.users.all():
                CHOICES2.append((x.name,x.name)) 

        CHOICES1 = [(x.name,x.name) for x in Project.objects.all()]

        print CHOICES2==CHOICES1 #this is True in this case

        self.fields['project']=forms.ChoiceField(choices=CHOICES2)

The form is loaded into the template with {{form.as_table}}. The form does not show a dropdown for the project field. Now the strange thing: if I change the last line to:

self.fields['project']=forms.ChoiceField(choices=CHOICES1)

it works, although the print statement of the "=="" comparison returns True (the lists are purposely the same - this is just for testing). I really have no idea how this can even work technically.

Edit - my project model:

class Project(BaseModel):
    name  = models.CharField(max_length=80)
    users = models.ManyToManyField(User)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I think you have to use queryset argument, which is mandatory: https://docs.djangoproject.com/en/1.8/ref/forms/fields/#django.forms.ModelChoiceField.queryset

ChoiceField must be declared with (queryset=None), and in the __init__ method you complete the query: https://docs.djangoproject.com/en/1.11/ref/forms/fields/#fields-which-handle-relationships

The problem could be about the execution order of the queries, or the cache of non-lazy queries.

And I agree with little_birdie: the field already exists.

over 4 years ago · Santiago Trujillo Relatório

0

Your field named project already exists and there's no need to construct another one as you are doing. It's better to just set the choices on the existing field:

self.fields['project'].choices = CHOICES2

But maybe you'd be better off using a ModelChoiceField:

project = ModelChoiceField(queryset=Project.objects.none())

and then set the queryset you want in init like so:

self.fields['project'].queryset=Project.objects.filter(users__in=[user])

..which should give you a list of all projects associated with user.

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