Overwrite Model's save method if you want something to be done just before inserting into the database
class Event(models.Model):
class Meta:
unique_together = ('name', 'held_date')
id = models.CharField(primary_key=True, max_length=20)
name = models.CharField(max_length=8)
held_date = models.DateField()
def save(self, **kwargs):
u"""Create the value of the primary key just before inserting"""
self.id = "%s@%s" % (str(self.held_date), self.name)
super(Event, self).save(**kwargs)
When I overwrite django's save (), the manager's method may or may not call it.
create
get_or_create
update_or_create
bulk_create
This does *not* call save() on each of the instances
It's surprisingly called.
There is also signals.pre_save
, but it's the same as doing it, and save
overwriting is more explicit.
Recommended Posts