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

Time:03-22

In 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)

But then on the Django admin page, I can't create a new Activation Type without selecting a Permitted Deployment Type. Furthermore, if I go into an existing Activation Type I am not sure how to select zero Permitted Deployment Types. I believe I am using Django 3.1.1

enter image description here

CodePudding user response:

You need to specify blank=True on the ActivationType model's permitted_deployment_types attribute.

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

See here.

CodePudding user response:

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:

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