0

I have a form with some fields that are not the Model's. i want to fill them out in my view before sending it to render..

my form:

class my_form(ModelForm):
    class Meta:
        model = my_model
        fields = ['name', 'color']
    non_model_field = forms.CharField(...)

my view code:

def my_view(request):
    a_model_obj = MyModel.objects.get()
    form = my_form(instance=a_model_obj)
    ## HERE I WANT TO DO SOMETHING LIKE this:
    ## form.non_model_form.set("myValue")
    ...

thanks

Mr. Nun.
  • 775
  • 11
  • 29
  • possible duplicate of [How to set initial values for a ModelForm when instance is also given](http://stackoverflow.com/questions/18254401/how-to-set-initial-values-for-a-modelform-when-instance-is-also-given) – Sayse Sep 02 '15 at 11:03

1 Answers1

1

Use this;

initial={'non_model_field':'value'}

MyForm(initial={'non_model_field':'value'})
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
  • Go through this link http://stackoverflow.com/questions/18254401/how-to-set-initial-values-for-a-modelform-when-instance-is-also-given – Geo Jacob Sep 02 '15 at 10:51