0

I have a form:

class TimingForm(forms.ModelForm):
class Meta:
    model = Timing
    fields = ('day','mng_start', 'mng_end', 'eve_start', 'eve_end')

I am making formset out of this form .

TimingFormSet = modelformset_factory(Timing, form=TimingForm, extra=7)

Here in the 'day' field of form I want the days of the week i.e. sun, mon... sat. Also I want to set it as that user cannot edit this field. I was about to use readonly field but came to know that djano's readonly field is not appriciated.

How can I make this possible. Setting initial value and making it uneditable.

1 Answer 1

3

change your form like this:

class TimingForm(forms.ModelForm):
    class Meta:
        model = Timing

    def __init__(self, *args, **kwargs):
        super(TimingForm, self).__init__(*args, **kwargs)
        self.fields['day'].widget.attrs['readonly'] = True

    fields = ('day','mng_start', 'mng_end', 'eve_start', 'eve_end')

for initialize forms with day of week you can use this:

day_of_week = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
formset = TimingFormSet()
for form, day in zip(formsets, day_of_week):
    form.initial['day'] = day

Now formset forms initialized with day of week in day field.

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

1 Comment

I think my formset will get the same initial value for all forms by doing this. I dont want that. And like I said readonly field is not appriciated so is there any other way to make it uneditable ??

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.