2

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?

juliomalegria
  • 24,229
  • 14
  • 73
  • 89

1 Answers1

2

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

.

bmihelac
  • 6,233
  • 34
  • 45
  • it would seem that post_save() is, though I don't know why they would leave that on... But I have UserProfile being created with a post_save signal and using loaddata later on causes some mismatches between user.id's and userprofile.userid's... – Filipe Pina Oct 25 '12 at 02:52
  • and to answer myself... checking for _raw_ seems to do the trick http://stackoverflow.com/questions/3499791/how-do-i-prevent-fixtures-from-conflicting-with-django-post-save-signal-code – Filipe Pina Oct 25 '12 at 02:54