How can I know in MyForm.clean() whether the data is new, or if already saved data is being modifed?
What should is_this_new_data() look like in the following code?
class MyForm(forms.ModelForm):
def clean(self):
cleaned_data = self.cleaned_data
if is_this_new_data(self):
# perform some checks if this is new data
else:
# do nothing if this is data being modifed
return cleaned_data
Check
self.cleaned_data['some_field']againstself.instance.some_field.A quick way to check if the object is new is to see if
self.instance.pkhas a value. It will beNoneunless the object already exists.