Reputation: 1245
I am using django 1.7 & python 2.7.
I have a model class which allows a text area and text box input for the user.
If
the user selects 0 in a select list, then
the text box is displayed to the user for input and the text area is hidden else
the text area is displayed and the text box hidden from the user.
Both the text box and text area have the exact same input. What ever is entered into the text box appears in the text area amd versa vise.
Both the text box and text area have different maxlength/max_length values.
The text box has a maximum length of 250 and the text area has a maximum length of 5000.
I have the client side validation working, but I am having difficulty getting the server side validation working.
How do I enable / disable the server side validation to my model on the forms.py file when the text box and text area have different max_length values, but the same input lengths?
I understand that the max_length can be assigned on the forms.py, but I cannot get the code syntax correct.
Here is my models.py code:
workplace_training_display_type = models.PositiveIntegerField(.....)
workplace_training_certificationTB = models.CharField(null=False, blank=False)
workplace_training_certificationTA = models.TextField(null=False, blank=False)
Normally I would drop the max_length=250
to the workplace_training_certificationTB
and the max_length=5000
to the workplace_training_certificationTA
on the above model fields. However, I believe that this has to be done dynamically in the forms.py file.
EDIT
If I set the max_length to the models for both the workplace_training_certificationTB
and workplace_training_certificationTA
, because both fields have exactly the same input, then one of the server side validations will be triggered, which is why I would like to set the values dynamically.
Here is my forms.py file code:
def clean(self):
cd_wrtdf = super(WorkplaceRelatedTrainingDetailsForm, self).clean()
if 'workplace_training_display_type' in cd_wrtdf and cd_wrtdf['workplace_training_display_type'] != 0:
# if the workplace_training_display_type is not zero, remove the max_length=250 from the textbox.
# if the workplace_training_display_type is not zero, add the max_length=5000 to the textarea.
else:
# if the workplace_training_display_type is zero, add the max_length=250 to the textbox.
# if the workplace_training_display_type is zero, remove the max_length=5000 from the textarea.
return cd_wrtdf
I have tried searching SO & Google, but could not find anything helpful.
Upvotes: 1
Views: 1227
Reputation: 902
You cannot have dynamic lengths in your model fields because these are implemented on the database level. When you set a max length on your field it translates it to the equivalent SQL that will limit the amount of data that can be stored on the database level.
As @electrometro mentioned I would also suggest having a single CharField in your model with a length of 5000.
Since this seems to be more a user interface problem and you should do this with Javascript and replace the input for the type you need based on the user selection you mentioned. As long as the form input posts the data with the same name the Django form will still function as expected.
You can do something similar to this toggle behavior except you will be triggering it when the user selects an option from your list. also make sure that the input/textarea ids and name are the same as the form field so the data will be able to be cleaned by the form.
Upvotes: 1
Reputation: 4158
Having duplicate fields on your model is not really the right way to do this. You should change your model to have only one field, a text field, with a max length of 5000. Then in your forms.py you can check the length of the input by using len(input)
. You can then decide if it is within the limits based on the other selected field.
In your clean method, calling super()
will automatically check if it is within 5000, then you will just check to see if it needs to be less than 250. If it is, then just see if the if len(input) > 250: raise ValidationError('Too long for type')
.
Upvotes: 2