I would like to display a warning message if I am into an editing form and hide it if I am in a creation form of a Django ModelForm.
form.is_bound tell me if the form was previously populated but how to test if the ModelForm was set with an existing instance ?
I tried this hasattr(form.instance, 'pk') but is it the right way to do so ?
Cheers,
Natim
Try checking if
form.instance.pkisNone.hasattr(form.instance, 'pk')will always returnTrue, because every model instance has apkfield, even when it has not yet been saved to the database.As pointed out by @Paullo in the comments, this will not work if you manually define your primary key and specify a default, e.g.
default=uuid.uuid4.