vergspi
vergspi

Reputation: 11

How can I combine model field and non-model field in ModelForm?

I have the following code:

class ExampleModel(models.Model):
    model_field = models.CharField()

class ExampleForm(forms.ModelForm):
    non_model_field = forms.HiddenInput()
    class Meta:
        model = ExampleModel
        fields = ('model_field', 'non_model_field',)

I get an exception:

FieldError

Unknown field(s) (non_model_field) specified for ExampleModel

How can I combine model field and non-model field in ModelForm?

Upvotes: 1

Views: 1157

Answers (1)

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13496

HiddenInput is a widget class, not a form field class. Use something like this instead if you want a hidden input field:

forms.CharField(max_length=100, widget=forms.HiddenInput, required=False).

Upvotes: 3

Related Questions