即使我在模型中设置了 "blank=True",Django Admin 中仍需要字段

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

在models.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)

但是在 Django 管理页面上,如果没有 select 允许的部署类型,我无法创建新的激活类型。此外,如果我进入现有的激活类型,我不确定如何 select 将允许的部署类型归零。我相信我正在使用 Django 3.1.1

您需要在 ActivationType 模型的 permitted_deployment_types 属性上指定 blank=True

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

参见 here

您不必在 DeploymentType 模型中添加 blank=True。您必须在 ActivationType 模型中将 blank=True 添加到 permitted_deployment_types。所以:

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