我以前从未使用过from django.db.models.signals import pre_save
@receiver(pre_save, sender=Shows)
def shows_receiver(sender, **kwargs):
#if writer kwarg is null don't override current data
class Poll(models.Model):
director = models.CharField(max_length=128, blank=True, )
writer = models.CharField(max_length=128, blank=True, )
,也没有找到任何好的例子。我想修改一个不应该为空的字段。如果给定字段已包含值,则不会发生任何事情。
https
答案 0 :(得分:0)
您可以通过修改传递给receiver的实例来实现它:
@receiver(pre_save, sender=Shows)
def shows_receiver(sender, instance, **kwargs):
if instance.some_field == "":
instance.some_field = "something"
或者对模型中的字段使用default值:
class Shows(models.Model):
some_field = models.CharField(max_length=128, default="something")