问题
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