Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

This field is required error in django default image

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading