5👍
✅
If you want the entry to be a text field, you can add a custom field to your ModelForm
, and make sure the default field is not shown by explicitly identifying which fields should be shown:
class WordForm(forms.ModelForm):
class Meta:
model = Word
hidden = ('language', )
fields = ('word',)
entries = forms.CharField()
Your form validation logic should be like this:
for form in formset:
obj = form.save(commit=False)
obj.language = # select the language here
obj.save()
entry = Entries.objects.get_or_create(entry=form.cleaned_fields['entries'])
obj.entries.add(entry)
obj.save()
Keep in mind with this implementation, you can’t edit fields using this form (since the character field will always be empty when the form is rendered).
Source:stackexchange.com