简体   繁体   English

即使我在 Model 中设置了“blank=True”,Django Admin 中仍需要字段

[英]Field is required in the Django Admin even though I have set "blank=True" in the Model

In models.py在模型.py

class DeploymentType(models.Model):
    deployment_type = models.CharField(primary_key=True, max_length=30, verbose_name="Deployment Type",blank=True)

    def __str__(self):
        return self.deployment_type

 class ActivationType (models.Model) :
    activation_type = models.CharField (
        primary_key=True,
        max_length=20,
        verbose_name = "Activation Type"
    )

    permitted_host_methods = models.ManyToManyField(
        HostMethod,
        verbose_name = "Permitted Host Methods"
    )

    permitted_deployment_types = models.ManyToManyField(
        DeploymentType,
        verbose_name = "Permitted Deployment Types" )

    class Meta:
         verbose_name = "Activation Type"

    def __str__(self):
        return str(self.activation_type)

But then on the Django admin page, I can't create a new Activation Type without selecting a Permitted Deployment Type.但是随后在 Django 管理页面上,我无法在不选择允许的部署类型的情况下创建新的激活类型。 Furthermore, if I go into an existing Activation Type I am not sure how to select zero Permitted Deployment Types.此外,如果我 go 进入现有的激活类型,我不确定如何 select 将允许的部署类型归零。 I believe I am using Django 3.1.1我相信我正在使用 Django 3.1.1

在此处输入图像描述

You need to specify blank=True on the ActivationType model's permitted_deployment_types attribute.您需要在ActivationType模型的permitted_deployment_types属性上指定blank=True

permitted_deployment_types = models.ManyToManyField(
        DeploymentType,
        verbose_name = "Permitted Deployment Types",
        blank=True)

See here .这里

You don't have to add blank=True in DeploymentType model. You have to add blank=True to permitted_deployment_types inside ActivationType model. So:您不必在 DeploymentType model 中添加 blank=True。您必须在 ActivationType model 中的 permitted_deployment_types 中添加 blank=True。因此:

    permitted_deployment_types = models.ManyToManyField(
        DeploymentType,
        #null=True,
        blank=True,
        verbose_name = "Permitted Deployment Types" )

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

相关问题 即使我设置null = True和blank = True,也会出现“此字段为必填”错误 - Getting “This field is required” error even though I set null=True and blank=True 完整性错误不是 NULL 约束失败,即使我在 model 中设置了空白=真,空=真 - Integrity Error NOT NULL constraint failed even though I have set blank=True, null=True in my model 尽管blank = True和null = True仍需要Django ForeignKey字段 - Django ForeignKey field required despite blank=True and null=True Django 模型 OneToOneField:管理员中的“此字段是必需的”错误 - Django model OneToOneField : "This field is required" error in admin Django图像没有文件关联,即使我有空白和null - Django image has no file associated even though I have blank and null 即使不需要,Django 文件字段更新也会导致错误 - Django file field update causing error even though not required 为什么不能将Django模型的USPhoneNumberField设置为null = True,blank = True? - Why can't I set my Django Model's USPhoneNumberField to null=True, blank=True? Django Admin 无法识别模型中的 blank=True - Django Admin doesn't recognise blank=True in model Django管理模型字段设置为当前用户 - Django admin model field set to current user 根据需要,Django Crispy表单设置模型字段 - Django Crispy form set model field as required
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM