您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Django-重写Model.create()方法?

Django-重写Model.create()方法?

__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)
Go 2022/1/1 18:24:45 有342人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶