Saturday, February 5, 2011

Re: model form validation

Shawn,

thanks for hint, I'll give that a try. Also I have some questions, for my own clarification of things: 

1) using your method - would is_valid() retur true then?
2) in my form display I should hide the "takeoff" field then, correct?
3) I alredy tried to override clean_takeoff() - it didn't get called. Any ideas, why?


Axel.



2011/2/5 Shawn Milochik <shawn@milochik.com>
On 02/05/2011 01:08 PM, Axel Bock wrote:
practically:
- the user enters "0300" in the datetime field.
- I make "0300" into date=0, time=03:00, valid field.

Axel,

In your example, you're wanting to populate a DateTime field in a model, but you want to allow the user to enter a string which would be invalid for the field.

One way to do this is to add an additional field to your form.

Example, given your 'takeoff' field:

   1. Add a charfield named takeoff_str in your form definition.

   2. Add a method named clean_takeoff_str to your form definition.

       In this function, check to ensure that the string entered is a valid
       HHMM value, if not raise a forms.ValidationError.

       Then, create a valid datetime from the input, and set
       self.cleaned_data['takeoff'] equal to this value.


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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


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

No comments:

Post a Comment