Search code examples
pythoninheritancedjango-formsdjango-viewsdjango-1.9

Django 1.9 form inheritance on UpdateView


The problem i have is with a single field that i added.

I have my form:



    class employeesForm(forms.ModelForm):
        class Meta:
            fields = [
                'num_employee',
                'id_ignition',
                'fullName',
                'shortName',
                'salary',
                'gender',
                'rfc',
                'imss',
                'curp',
                'birthday',
                'initday',
                'id_costCenter',
                'id_jobCode',
                'id_status',
                'nationality',
                'picture',
                'source',
            ]


            widgets = {
                'num_employee': forms.NumberInput(attrs={'class': 'form-control', 'name': 'num_employee'}),
                'id_ignition': forms.NumberInput(attrs={'class': 'form-control', 'name': 'id_ignition'}),
                'fullName': forms.TextInput(attrs={'class': 'form-control', 'name': 'fullName', 'placeholder': 'Angel Rafael Ortega Vazquez'}),
                'shortName': forms.TextInput(attrs={'class': 'form-control', 'name': 'shortName', 'placeholder': 'Rafael Ortega'}),
                'salary': forms.NumberInput(attrs={'class': 'form-control', 'name': 'salary', 'placeholder': '5000'}),
                'gender': forms.CheckboxInput(attrs={'class': 'form-control', 'name': 'gender'}),
                'rfc': forms.TextInput(attrs={'class': 'form-control', 'name': 'rfc', 'id': 'rfc'}),
                'imss': forms.TextInput(attrs={'class': 'form-control', 'name': 'imss', 'id': 'imss'}),
                'curp': forms.TextInput(attrs={'class': 'form-control', 'name': 'curp'}),
                'birthday': forms.DateInput(attrs={'class': 'form-control', 'name': 'birthday'}),
                'initday': forms.DateInput(attrs={'class': 'form-control', 'name': 'initday'}),
                'id_costCenter': forms.Select(attrs={'class': 'form-control', 'name': 'id_costCenter'}),
                'id_jobCode': forms.Select(attrs={'class': 'form-control', 'name': 'id_jobCode'}),
                'id_status': forms.Select(attrs={'class': 'form-control', 'name': 'id_status'}),
                'nationality': forms.Select(attrs={'class': 'form-control', 'name': 'nationality'}),
                'picture': forms.ClearableFileInput(attrs={'class': 'form-control', 'name': 'picture'}),
                'source': forms.Select(attrs={'class': 'form-control', 'name': 'source'}),
            }

In my model I have an extra field (antiquity), but I’m ignoring it here because in my CreateView i populate that field based in other parameters (antiquity = initday).

Everything worked fine until I needed to create an UpdateView with that extra field pre-populated, what I did was to inherit the previews form and adding that extra field:



    class Enhanced_employeesForm(employeesForm):

        antiquity = forms.CharField(
            widget=forms.DateInput(attrs={'class': 'form-control', 'name': 'antiquity'}))

It did the thing, the input field is rendered in my template, but while everything is pre-populated with the info based in the id, my antiquity field is empty.

That’s the only thing I’m missing in my configuration, because django even detect when that field is empty on the submit and prevent any update to the database

I tried doing something like:

form.antiquity(instance=Employees.objects.get(pk=self.kwargs['pk']))

But the error says that 'Enhanced_employeesForm' object has no attribute 'antiquity'.


Solution

  • I was doing it wrong, i had to add this code into my enhanced form:

    class Enhanced_employeesForm(employeesForm):
        class Meta(employeesForm.Meta):
            employeesForm.Meta.fields += ['antiquity']
    

    and that made the thing.

    if you need widgets you might noticed that everything is overriden, to prevent that you'll need this:

    employeesForm.Meta.widgets['antiquity'] = forms.DateInput(attrs={'class': 'form-control'})