Home > Enterprise >  This field is required error in django default image
This field is required error in django default image

Time:12-09

I set my imagefield as NONE but when i tried to enter data without image from django admin panel it is showing that This field is required.

this is my urls.py `

class dish(models.Model):
    dish_id = models.AutoField
    dish_name = models.CharField(max_length=255)
    dish_size = models.CharField(max_length=7)
    dish_price = models.IntegerField()
    dish_description = models.CharField(max_length=255)
    dish_image = models.ImageField(upload_to="", default=None)
    dish_date = models.DateField()

    def __str__(self):
        return self.dish_name

` How to set it as none if user not select any image

CodePudding user response:

dish_image = models.ImageField(upload_to="", default=None, blank=True, null=True)

Both blank=True and null=True are needed. Here’s why: What is the difference between null=True and blank=True in Django?

——-

Also, you don’t need dish_id. IDs are automatically generated and accessed with model_instance.pk

https://docs.djangoproject.com/en/4.1/topics/db/models/#automatic-primary-key-fields

  • Related