简体   繁体   English

如何使 ESIdentityCardNumberField 允许空白

[英]How to make ESIdentityCardNumberField allow blank

I've got a form to register clients.我有一个表格来注册客户。 There I've got one ESIdentityCardNumberField to validate NIF/CIF/NIE, but this form is also considered to allow "Other" country Cards IDs, so I've added another field to get them.我有一个 ESIdentityCardNumberField 来验证 NIF/CIF/NIE,但是这个表单也被认为允许“其他”国家卡 ID,所以我添加了另一个字段来获取它们。

So, I need to override ESIdentityCardNumberField validation to allow blank values if other card id is specified.因此,如果指定了其他卡 ID,我需要覆盖 ESIdentityCardNumberField 验证以允许空白值。

How can I get this?我怎样才能得到这个?

class Client(models.Model):
    name = models.CharField()
    ESCardId = models.CharField(blank=True, null=True)
    otherCardsIds = models.CharField(blank=True, null=True)

class ClientForm(forms.ModelForm):
    ESCardId= ESIdentityCardNumberField()


class ClientAdmin(admin.ModelAdmin):
    form = ClientForm
    fields = ['name', 'ESCardID', 'otherCardsIds']

    def save_model(self, request, obj, form, change):
        if obj.ESCardId is None and obj.otherCardsIds is None:
            raise ValidationError("Enter a spanish cardID or any other.")

Thanks in advance.提前致谢。

I have noticed bad point of view.我注意到了不好的观点。 Don't use a form field but a validator, like:不要使用表单字段,而是使用验证器,例如:

class ClientForm(forms.ModelForm):

    def clean(self):
        cleaned_data = self.cleaned_data
        CIF = cleaned_data.get('CIF')
        oID = cleaned_data.get('otherCIF')
        if CIF == "" and oID == "":
            raise forms.ValidationError("Introduce one card id")
        if CIF != "" and oID != "":
            raise forms.ValidationError("Introduce ONLY one card id")
        if CIF != "":
            myValidator = ESIdentityCardNumberField()
            myValidator.clean(CIF)

        return cleaned_data
    class Meta:
        model = Client
ESIdentityCardNumberField(label='DNI/CIF', required=False)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM