简体   繁体   English

Python Django - Select 选择无效

[英]Python Django - Select Choice is not Valid

When I want to save my form on a new CharFields (with Choices), Django tell me that the Select choice is not valid.当我想将表单保存在新的 CharFields(带有选项)上时,Django 告诉我 Select 选项无效。 When I take another Choice list which works with other models, it does work normally.当我选择另一个适用于其他模型的选择列表时,它确实可以正常工作。

Models.py:模型.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: 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,.... .所以我的字段 ProgramEG 工作正常,但是当我尝试放置(选择)列表以确保字段 Probecard 工作时,它停止工作并告诉我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).编辑:我解决了我的问题:我更改了 Probecard def init (self, *args, **kwargs): super(ProducDefForm, self) 的 model 字段。 init (*args, **kwargs) ListProbecard = Utilities_CSP.ExtractListCAPChoice() self.fields['Probecard'] = forms.ChoiceField(choices=ListProbecard) init (*args, **kwargs) ListProbecard = Utilities_CSP.ExtractListCAPChoice() self.fields['Probecard'] = forms.ChoiceField(choices=ListProbecard)

Thankd for your time of view.感谢您的观看时间。

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)我解决了我的问题:我更改了 Probecard def init (self, *args, **kwargs): super(ProducDefForm, self).init(*args, **kwargs) ListProbecard = Utilities_CSP.ExtractListCAPChoice() self 的 model 字段.fields['Probecard'] = forms.ChoiceField(choices=ListProbecard)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django形式错误:RadioSelect()上的“选择有效选择” - Django form error: “Select a valid choice” on RadioSelect() 选择一个有效的选项。 该选择不是可用的选择之一——Django 表单 - Select a valid choice. That choice is not one of the available choices --Django forms Django:Select 是一个有效的选择。 该选择不是可用的选择之一 - Django: Select a valid choice. That choice is not one of the available choices Django给出错误“选择有效选择”。 Django 1.11.5版 - Django gives error “Select a Valid choice” . Django Version 1.11.5 Django ModelForm 中的错误。 选择一个有效的选项。 该选择不是有效选择之一 - Error in Django ModelForm. Select a valid choice. That choice is not one of the valid choices django modelformset_factory 引发表单无效:id 选择一个有效的选择。 该选择不是可用的选择之一 - django modelformset_factory raises form not valid: id Select a valid choice. That choice is not one of the available choices 选择一个有效的选择ModelChoiceField - Select a valid choice ModelChoiceField Django表单错误:选择一个有效的选项。 ......不是可用的选择之一 - Django Form Error: Select a valid choice. … is not one of the available choices “选择一个有效的选择。” Django中使用Modelform时出错 - “Select a valid choice.” Error using Modelform in Django Django表单对于ChoiceField始终无效-选择有效的选择 - Django Form is Always Invalid with ChoiceField - Select a Valid Choice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM