Python Django – Select Choice is not Valid

Question:

When I want to save my form on a new CharFields (with Choices), Django tell me that the Select choice is not valid.
When I take another Choice list which works with other models, it does work normally.

Models.py:

class productsdefinition(models.Model):
    Products = models.CharField(max_length=Lenght200, default='NA')
    Status = models.CharField(max_length=Lenght10, choices=Utilities_CSP.PRODENG_CHOICES)
    Tester = models.ForeignKey(tester, on_delete=models.CASCADE, null=True)
    Board = models.ForeignKey(board, on_delete=models.CASCADE, null=True)
    TestProgram = models.CharField(max_length=Lenght200, default='')
    ProgramLoad = models.ForeignKey(ProgramLoad, on_delete=models.CASCADE, null=True)
    ListProgramEG = Utilities_CSP.ExtractProgEG()
    Probecard = models.CharField(verbose_name='Probe Card',max_length=Lenght200, choices=ListProgramEG, null=True)
    ProgramEG = models.CharField(verbose_name='Program EG',max_length=Lenght200, choices=ListProgramEG, null=True)

forms.py:

class ProducDefForm(forms.ModelForm):
    global tempfield
    i = 0
    tempfield = []
    
    for var in list(vars(productsdefinition()).keys()):
        if i <= 1:
            pass
        elif var == "Date_added":
            pass
        elif var == "LastModified":
            pass
        elif var.find("_id") != -1:
            var = var.replace("_id","")
            tempfield.append(str(var))
        else:
            tempfield.append(str(var))
        i = i + 1
    class Meta:
        model = productsdefinition
        fields = tempfield

    def __init__ (self, *args, **kwargs):
        super(ProducDefForm, self).__init__(*args, **kwargs)
        ListProbecard = Utilities_CSP.ExtractProgEG()
        self.fields['Probecard'].choices = ListProbecard

So my field ProgramEG works fine, but when I try to put the list (of choices) to ensure that the field Probecard work, it stops working and it tells me Select Choice in not valid,.....
Do you have any idea where the error can be? Is it in the table of the database?

EDIT:
I solve my problem: i change the model fields of th Probecard
def init (self, *args, **kwargs):
super(ProducDefForm, self).init(*args, **kwargs)
ListProbecard = Utilities_CSP.ExtractListCAPChoice()
self.fields[‘Probecard’] = forms.ChoiceField(choices=ListProbecard)

Thankd for your time of view.

Asked By: William

||

Answers:

I solve my problem: i change the model fields of th Probecard def init (self, *args, **kwargs): super(ProducDefForm, self).init(*args, **kwargs) ListProbecard = Utilities_CSP.ExtractListCAPChoice() self.fields[‘Probecard’] = forms.ChoiceField(choices=ListProbecard)

Answered By: William
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.