Reputation: 1887
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
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