当使用loaddata进行固定时,模型的save()方法是否被调用? [英] Does a model's save() method get called when using loaddata for fixtures?

查看:140
本文介绍了当使用loaddata进行固定时,模型的save()方法是否被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个字段中,我试图为模型生成一个空白的自动插件。这是代码:

I'm trying to generate an automatic slug for a model whenever it is empty, from another field. This is the code:

class Position(RichText):
    name = models.CharField(max_length=200)
    slug = models.SlugField(null=True)

    def position_description(self):
        return self.content

    def __unicode__(self):
        return self.name

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)

        super(Position, self).save(*args, **kwargs)

当我使用loaddata加载初始固定装置时,似乎从未触发 save()方法。这是正常的行为吗?

When I load initial fixtures with loaddata, it seems the save() method is never triggered. Is this normal behavior? How can I catch fixtures too?

推荐答案

这是正常的行为,从文档


当夹具文件被处理时,数据将以
的形式保存到数据库。模型定义的保存方法和预分频信号不称为

When fixture files are processed, the data is saved to the database as is. Model defined save methods and pre_save signals are not called

这篇关于当使用loaddata进行固定时,模型的save()方法是否被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆