Reputation: 6555
I'm hoping this makes sense, here is what I want to do (for whatever reason).
I would like to create a non-model form field within my model form?
For example, below I have a model form, I would like at init to add an extra field that's not contained within the model called template. I don't save this new field so it does not need to be in my model or to I want it to be (I do some fancy ajax stuff with it.).
forms.py
class testForm(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
super(testForm, self).__init__(*args, **kwargs)
if user is not None:
this_templates = Template.objects.for_user(user)
self.fields["templates"] = forms.??????????
Upvotes: 4
Views: 2555
Reputation: 18982
You are almost there:
self.fields["templates"] = forms.CharField()
Or if you prefer another field type check the documentation for possible options.
And for additional background information take a look at Overloading Django Form Fields.
Upvotes: 6