syabro
syabro

Reputation: 1887

How to validate Django model in form without submit?

I need to validate Model somewhere deep in code. F.e. for grant access to a few pages.

How can I validate ModelForm against model instance without generating fake POST/GET data?

Upvotes: 1

Views: 885

Answers (1)

Kari-Antti Kuosa
Kari-Antti Kuosa

Reputation: 376

You can pass dictionary for you form and validate form after it.

form = YourForm (your_dict)
form.validate ()

You can also generate dict automatically from django model by this:

from django.forms import model_to_dict

your_dict = model_to_dict(your_object
    fields = ["fields to include"]
    exclude = ["fields to exclude"]
)

Upvotes: 3

Related Questions