加载 Django 装置时未调用 model.save() ?
我正在重写 Django 模型 save() 方法,因此我可以对对象进行一些额外的健全性检查。 (save() 是执行此操作的正确位置吗?)
我的fixtures/initial_fixtures.yaml 对象似乎没有调用其save() 方法。我如何检查我的灯具?
I am overriding my Django model save() method, so I can do some extra sanity checking on the object. (Is save() the correct place to do this?)
It doesn't appear that my fixtures/initial_fixtures.yaml objects have their save() method called. How can I sanity-check my fixtures?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 Django 1.5 开始,save() 不会被调用:
https://docs.djangoproject.com/en/1.9/ref/django-管理员/
As of Django 1.5, save() is NOT called:
https://docs.djangoproject.com/en/1.9/ref/django-admin/
.save()
方法在夹具加载期间被调用,如 https://code.djangoproject.com/browser/django/tags/releases/1.3.1/django/core/management/commands/loaddata.py?rev=17029#L174如果如果你使用不同的 DJ 版本,你可以检查一下,但我很确定它在旧版本中也被调用。
您如何检查对象是否调用了
save()
方法?关于在
.save()
中执行此操作,如果健全性检查非常重要,那么我认为这不是一个好主意。The
.save()
method is called during fixture loading as seen in https://code.djangoproject.com/browser/django/tags/releases/1.3.1/django/core/management/commands/loaddata.py?rev=17029#L174If you use a different DJ version, you can check that but I'm quite sure it is called in older versions as well.
How are you checking if your objects have their
save()
method called?And about doing this in
.save()
, if the sanity checks are non-trivial then I don't think it's a very good idea.您的灯具被认为是良好的数据,而不是有问题的输入,因此我不确定您何时需要对它们进行健全性检查。
如果您需要进行一些一次性初始验证,您可以通过管理员或应用程序中的其他方式将数据添加到数据库,然后将其导出为固定装置。
Your fixtures are assumed to be good data, not questionable input, so I'm not sure of a good case when you would need to be sanity checking them.
You can add data to your db through the admin or something within your app and then export that as a fixture, if you need to do some one-time initial validation.