Mr. Nun.
Mr. Nun.

Reputation: 850

django manually set field value

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

Upvotes: 1

Views: 1332

Answers (1)

Geo Jacob
Geo Jacob

Reputation: 6009

Use this;

initial={'non_model_field':'value'}

MyForm(initial={'non_model_field':'value'})

Upvotes: 1

Related Questions