Enforce validator on creation only #10590
Answered
by
kkthxbye-code
domainzero
asked this question in
Q&A
-
There's probably a better way to do this, but I might be too blind to see it. I have a custom validator to enforce uniqueness on device names with tenants, but the validator prevents me from editing existing objects as the name already exists. Is there a way to only enforce this validator on device creation versus update? My validator looks like this: class DeviceNameUnique(CustomValidator):
def validate(self, device):
existing_devices = Device.objects.filter(name__iexact=device.name).exists()
if existing_devices:
self.fail("Device name in use.", field="name") |
Beta Was this translation helpful? Give feedback.
Answered by
kkthxbye-code
Oct 6, 2022
Replies: 1 comment 5 replies
-
You should be able to do:
device.pk will only be None if it's a creation. |
Beta Was this translation helpful? Give feedback.
5 replies
Answer selected by
domainzero
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should be able to do:
if not device.pk:
device.pk will only be None if it's a creation.