Django model field validator function used for update or insert [closed]

纵饮孤独 提交于 2019-12-11 19:13:42

问题


Is there a way of figuring out inside a Django model field validator function if it used for update or insert?


回答1:


No, there isn't. A validator is simply a callable that takes a value and checks it. It has no access to the field or instance being validated, so it is impossible to tell whether it is a new or existing instance.




回答2:


Not sure if it's what you're looking for, but I generally use self.pk to determine whether a model has already been saved or not. If there's no primary key, I can assume it's an insert.



来源:https://stackoverflow.com/questions/12665153/django-model-field-validator-function-used-for-update-or-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!