此字段是必需的django默认图像错误

62lalag4  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(115)

我把我的imagefield设置为NONE,但是当我试图从django管理面板输入没有图像的数据时,它显示这个字段是必需的。
这是我的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

'如果用户没有选择任何图像,如何将其设置为无

6rqinv9w

6rqinv9w1#

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

相关问题