Django model validation in clean or in separate validators? - django

I am pretty new to Django and I was wondering what is the best method to validate your models in Django? Are there any advantages to using clean vs adding validators to your models? By validators I mean when you do declare the field, populating the validator=[] list. I find the models to be easier to read with the validators in another package, and declared in the field list. Also, I try to validate the data before in services, before initializing the models. What do you think / prefer? Thanks a lot.

The difference is that a validator will apply to model objects created in forms and directly, i.e.:
form = MyModelForm(...
# and
my_model = MyModel(...
Whereas clean will only apply to a form. If you are creating model objects outside of forms then you should consider using validators. You might also find using validators to be a cleaner, more DRY method of checking what is created rather than overriding form clean functions.

Related

Serializer Equivalent of clean for django forms in rest framework Serializers

What is the serializer equivalent of the clean method for django forms.
My use case is that I want to do custom model field validation of some fields for a model serializer class. My guess is it could be the run_validators, for the framework. But I am not sure with certainty.
Thanks,
Best regards,
Well, just gone through the documentation and the equivalent is validate (for object level validation). For field level validation use. validate_<field_name>'
See link for more details

django use ModelForm to validate a Model

Suppose I have a ModelForm for my Model with custom validation logic, and I want to reuse that to validate an existing model instance. Is there any way to do this?
I thought I could just do something like MyModelForm(instance=foo).is_valid(), but that doesn't work for multiple reasons.
I'd like to reuse the existing form validation to avoid duplication of code, but I can't find any way to do so.
Edit: I'm using Django 1.10, in case that makes a difference.

Use cases for overriding save method in Django

Suppose I have a ModelForm and want to save the form. I know that we can override save method in django in the forms and models python code. But I want to know when exactly we should use saving forms in views, override save in forms and override save in models?
There's no right answer.
All you can do is choosing case by case:
Forms:
Specific methods will be used specifically when model is saved using this form.
Models:
Method will (almost) be used when saving models.
A third way you don't mention is Signals.

Storing value in django models without using Model forms

I am having very complex models in my project, that I cannot display easily on HTML page using modelForms.
So I want to to use normal forms instead and store the values in Models.is it possible?
It is possible. You simply create your own form by inheriting forms.Form.
Then get the form input from request in your views and create your object.
In short what you are asking is possible.
Here is the official documentation for forms: https://docs.djangoproject.com/en/1.8/topics/forms/

Django forms lose related data

It appears that there's no way for me to get an ORM object with numerous select_related objects and create a Form and run my data through all the cleaning and additional validations I have.
I'm simply trying to reduce the number of queries that get executed, the form either takes an id and re-creates the object, or does not take advantage of the additional fields (I've tried using model_to_dict)
Is there anything I'm missing? How can I use select_related inside of a form?
Django 1.4
Do:
form = MyFormClass(instance=my_object)
Your form will need to be a ModelForm, see the docs for more information!