I have a form where the fields are coming from a ModelForm, and one of them is a models.ForeignKey field that is linked to usernames in my User model. This results in every user in the database being listed in a selection field. I’d prefer that this field be a TextInput where someone enters a username, and I’ve made it that in the ModelForm. But when my view goes through form.is_valid(), the form gives an error saying that this field is invalid. If I remove the is_valid() check, it of course gives me an error that the form cannot be saved because the data didn’t validate.
How can I validate and save TextInput to my ForeignKey field?
Override your form clean method like here https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-clean-method
Make sure your clean method cleans the textfields (strip whitespaces) and lookup that the value of the username (or user id) are actually correct.