社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

Django在默认情况下放置空字符串,但在DB列中除外

Sazzadur Rahman • 5 年前 • 1565 次点击  

我正在使用 postgresql 版本10.6 Django 2.1应用。问题是当我使用 null=True 在我的模特领域里 空字符串(“”) 在数据库列中,我尝试将其默认为 null .

在下面的代码示例中 image 列应该是 无效的 :

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='profile_pics', null=True, blank=True) 

我把这个班叫做 signal 这样地:

@receiver(post_save, sender=User)
def create_profile(sender, **kwargs):
    if kwargs.get('created') is True:
        Profile.objects.create(user=kwargs.get('instance'))

在这个快照中你可以看到 形象 列作为空字符串插入,而不是 无效的

enter image description here

我还尝试将模型字段的默认值用作:

image = models.ImageField(default=None, upload_to='profile_pics', null=True, blank=True)

但也不管用。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43437
 
1565 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Kaan Karaca
Reply   •   1 楼
Kaan Karaca    6 年前

无效的: 它与数据库相关。定义给定数据库列是否接受空值。

Blank: 它与验证相关。当调用form.is_valid()时,它将在表单验证期间使用。

null和blank的默认值为false。

你应该移除 blank=True

Exprator
Reply   •   2 楼
Exprator    6 年前
image = models.ImageField(upload_to='profile_pics', null=True) 

移除 blank=True