1

I've a specific html input element that shows a calendar, it is possible to add this element to my form and be validated? i exclude the model field in my form, so when i render not be used in the form fields

class AddVisionForm(forms.ModelForm):
    class Meta:
        model = Visions
        exclude = ('init_date',
                   )

My approach is to pass the post to the save method in the form: form.save(request.POST) and save the value to the instance:

def save(self, *args, **kwargs):
    instance = super(AddVisionForm, self).save(commit=False)
    post = args[0]
    instance.init_date = post['init_date']
    instance.save()

But this is causing that if a user put other values than the required date format crash the site

2 Answers 2

1

Instead exclude your init_date model field, you can change the widget of the init_date (probably a hidden input) and populate it using javascript:

class AddVisionForm(forms.ModelForm):
    class Meta:
        model = Visions
        widgets = {'init_date': forms.HiddenInput()}

And you can take approach from the field validation process.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Meta widgets in the form class to customize your html inputs

For example

class AddVisionForm(forms.ModelForm):
    class Meta:
        model = Visions
        widgets = {
            'init_date': TextInput(attrs={'class': 'date-calendar'}),
        }

You can handle the input with the selector '.date-calendar' and transform that input into a calendar with your preferred javascript function, like jquery-ui calendar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.