mardi 1 janvier 2019

Django model objects do not load in Form in testing environment

Using Django 2.1.3

Getting a strange error here; I have a form multiplechoicefield that draws its choices from the values existing in a model in the database.

class ChartForm(Form):
    P_CHOICES = tuple((p["p"], p["p"]) for p in VAR.objects.all().values("p"))

    p = MultipleChoiceField(widget=CheckboxSelectMultiple, choices=P_CHOICES, initial=P_CHOICES[0][1])

I am trying to run tests for a different app in the project. It throws the following error:

  File "/code/pyyc/forms.py", line 31, in ChartForm
    p = MultipleChoiceField(widget=CheckboxSelectMultiple, choices=P_CHOICES, initial=P_CHOICES[0][1])
IndexError: tuple index out of range

I assumed it was just because the model objects weren't loaded. So I added in the fixtures from the VAR app.

And yet, it still throws the error. Presumably, the Form is render before the test database is compiled ... ?

So I am now editing the Form so that P_CHOICES is done manually, but this is obviously not ideal for the test environment.

Anyone ever come across this? Is there a smart hack for this that doesn't involve commenting out lines in the Form every time you want to test?

Aucun commentaire:

Enregistrer un commentaire