问题
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