--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
class roForm(forms.Form):
def __init__(self, *args, **kwargs):
self.readonly = 'readonly' in kwargs
if self.readonly:
kwargs.pop('readonly')
super(roForm, self).__init__(*args, **kwargs)
if self.readonly:
for key in self.fields.iterkeys():
self.fields[key].widget.attrs['disabled'] = True
regards
Steve
1. Override __init__() and store the value of that field in a
temporary value, such as self.old_status.
2. Override save() and before you call the super() save, check if
self.pk (meaning the instance is not new). If so, and self.old_status
!= self.current_status, you can raise an exception.
For extra credit you might want to tweak your ModelForm so that it
will raise a ValidationError, because otherwise it'll validate and
then you'll get a 500 error when the save() on the model is attempted.
However, I like having the exception raised at the model level because
it will protect your data in case the model is accessed by some means
other than your ModelForm.
Shawn
I agree. That's not incompatible with my solution. I would expect the
user to be informed of the policy rather than just have changes
ignored.
I chose to present the simplest possible explanation you could use to
do what you wanted instead of going into great depth writing your
ModelForms for you because it was easier to do. I take for granted
that there is more than I know (or will ever know) going on in your
project and I expect that you will ignore or adapt my suggestions as
you see fit.
Shawn