Py学习  »  Django

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

Sazzadur Rahman • 5 年前 • 1563 次点击  

我正在使用 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
 
1563 次点击  
文章 [ 2 ]  |  最新文章 5 年前