Quick Start¶
Requirements¶
python >= 3.6
django >= 2.2
djangorestframework (tested against 3.11)
Installation¶
installing with pip
pip install django-data-validation
In your project, add rest_framework and datavalidation to INSTALLED_APPS
INSTALLED_APPS = (
...
"rest_framework",
"datavalidation.apps.DataValidationConfig",
...
)
from your project directory run the database migrations
./manage.py migrate datavalidation
When running the django-admin server the static files for the datavalidation admin will be served automatically (assuming “django.contrib.staticfiles” is in INSTALLED_APPS). Otheriwse, you should also run
./manage.py collectstatic
Basic Usage¶
On any django model that has data that you would like to validate, add a method decorated with @data_validator that returns PASS, FAIL or NA. For instance if you have a model with a start and end time, you can add a data_validator to check that the start time is always before the end time
from django.db import models
from datavalidation import data_validator, PASS, FAIL, NA
class YourModel(models.Model):
...
start_time = models.DateTimeField()
end_time = models.DateTimeField(blank=True, null=True)
...
@data_validator
def check_start_time(self):
""" check that the start time is before end time """
if self.end_time is None:
return NA("end time not set")
elif self.start_time < self.end_time:
return PASS
else:
return FAIL("end time is before start time!")
To run the validation for all models
./manage.py validate
or for a specific model
./manage.py validate yourapp.YouModel
See Writing Data Validators for more details and examples of data validators
Optionally, you can add the data_validaiton.models.DataValidationMixin to your models to provide some additional methods for querying the validation results
from datavalidation.models import DataValidationMixin
class YouModel(DataValidationMixin, models.Model):
...
# in a shell
print(YouModel.datavalidation_status)
>>> Status.PASSING # hopefully :)
Finally, you can also add data_validaiton.admin.DataValidationMixin to your django admin classes to review the data in the admin. See Setting up the Admin for details.