__init__()
每当实例化对象的python表示形式时,覆盖将导致执行代码。我不知道rails
,但是一个:before_created
过滤器听起来像是在数据库中创建对象时要执行的代码。如果要在数据库中创建新对象时执行代码,则应重写save()
,检查该对象是否具有pk属性。代码看起来像这样:
def save(self, *args, **kwargs):
if not self.pk:
# This code only happens if the objects is
# not in the database yet. Otherwise it would
# have pk
super(MyModel, self).save(*args, **kwargs)