Does a model's save() method get called when using loaddata for fixtures?

帅比萌擦擦* 提交于 2019-12-01 08:25:23

问题


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)

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?


回答1:


This is normal behavior, from the documentation:

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

.



来源:https://stackoverflow.com/questions/8595536/does-a-models-save-method-get-called-when-using-loaddata-for-fixtures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!