I have been learning about how forms, and now ModelForms, work.
In a video by Max Goodridge, he redefines a field for one of his ModelFields in his ModelForm class. That is, he manually adds a field to his ModelForm class that could have been auto-generated by the ModelForm framework. From what I have read and understood thus far, that may be something to avoid. Though, that is not where my question lies.
I am wondering how redefining fields within a ModelForm class works. In the Django Docs, it is stated (with an example) that a ModelForm instance will have a form field for every model field specified. What happens then, when a form field is explicitly defined in a ModelForm instance? Are two fields generated or does ModelForm recognise that a field is already defined, thus not generating another one?
Furthermore, what exactly does adding an attribute to a ModelForm instance in the views do? For example, I have seen this:
form = ExampleForm(request.POST)
if form.is_valid():
post = form.save(commit=False)
post.user = request.user # herein lies my confusion
post.save()
What exactly is happening here? I have seen people do this and adding a timestamp as well, but I fail to understand exactly what it does. Presumably, the .save() method recognizes the attribute name 'user' and adds it to the database if the name corresponds with a Model-field name 'user'?
What happens when a form field is explicitly defined in a
ModelForminstance?How does adding an attribute with information for a model-field in a
ModelForminstance work?
Thank you!