Hi,

Thank you very much for your help.

I used your form validation method.

The results is weird.

I tested it with a value lower than the previous value. But didn't see
any error message in my browser.

I stepped through the code using pdb. I could see that
forms.ValidationError("The amount in field yourfieldname may only
increase.")
was raised.

When I looked at the view that processes the form, I could also see
that form.is_valid() returned False.
Yet the next lines executed were:
variables = RequestContext(request, {'form': form})
return render_to_response('template.html', variables)
Those are the lines to be executed if there is no POST data.

I thought if form.is_valid() returns False, Django would send a list
of the errors to browser. But that's not what happened here.

Any ideas why?

On Dec 6, 11:20 pm, Shawn Milochik <sh...@milochik.com> wrote:
> On Dec 6, 2009, at 11:01 PM, Continuation wrote:
>
> > What do you mean by the __init__ of the model?
>
> Every models.Model object has an __init__ function, like any Python class. 
> You need to override it.
>
> Add this into your model (in your models.py):
>
>     def __init__(self, *args, **kwargs):
>
>         #make sure you call the default __init__, or stuff breaks.
>         super(YourModelName, self).__init__(*args, **kwargs)
>
>         #so we can tell if the new value for the field
>         #is increased upon save
>         self.old_value = self.field_name
>
> Then, you override the save function for the same model:
>
>     def save(self):
>
>         if self.old_value < self.fieldname:
>             raise ValueError('The field x can only increase.')
>
>         #go through with the standard 'save' stuff
>         #that is defined in models.Model class.
>         super(YourModelName, self).save()
>
> This way, your model will not allow itself to be saved if that value is 
> lowered. This doesn't directly answer your question, which was about form 
> validation. I don't know how to do it elegantly, so I hope someone else will 
> chime in. However, one thing you can do is to override the "clean" function 
> of your form for that particular field so that it pulls back an instance of 
> the old object and checks its own cleaned_data value against the value stored 
> in the database. Something like this:
>
> #within your forms.ModelForm for the model in question
>
>     def clean_yourfieldname(self):
>
>         yourfieldname = self.cleaned_data.get('yourfieldname', 0)
>         old_instance = YourModel.objects.get(pk = self.instance.pk)
>
>         if yourfieldname < old_instance.yourfieldname:
>             raise forms.ValidationError("The amount in field yourfieldname 
> may only increase.")
>
>         return yourfieldname
>
> To be fair, if you use this in your form validation, you don't really need 
> the stuff above in your model. However, if that is your business logic, then 
> it's a good idea to have it there in any case.
>
> Shawn

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to