Skip to main content

A simple library that enhance 'ModelSerializer' class so that it performs object-level validation for free.

Project description

drf-model-serializer

Description

The scope of this module is to enhance DRF's ModelSerializer class, and provide object-level validation out of the box.

The idea is that the model defines its object-level validation in its clean method. The serializer class ModelSerializer provided by this module, overrides its validate method so that it invokes the model's clean method. Therefore whenever invoking the serializer's is_valid method, the validate method runs and provides the object-level validation.

This way, object-level validation is defined once in the model and is used automatically by the Django admin, the model when explicitly invoking full_clean and the serializer whenever invoking is_valid. This follows the convention for field-level validation. When defined correctly using model validators, the field-level validation is used out of the box by the Django admin, serializer and model.

Proving the validation idea

In this module, we also provide a Django project that will show you how the object level validations works together with Django admin, Serializer, and a model by implementing drf-model-serializer and model clean() method. A main focus of the test project lives on create/update actions over any drinks or main dishes recipe, which all any incoming data either from Admin or Serializer must be validated. See the Recipe model and the tests section to understand it in more detail.

Prerequisite

Ensure pip installed on your local machine. If you don't have it, you can follow the installation guide here.

How to run the tests

  1. Open terminal and go to the root directory of where the package live, for instance: $ cd Documents/py-projects/drf-model-serializer.
  2. Run $ ./scripts/requirement-install.sh from your command line.
  3. Run $ ./scripts/checker.sh.

How to run the test project

Currently only Recipe admin page that available to be explored, and to try it out you can follow these steps:

  1. Open terminal and go to the root directory of where the package live, for instance: $ cd Documents/py-projects/drf-model-serializer.
  2. Run $ ./scripts/requirement-install.sh from your command line (you can skip this if you already did it when running the tests).
  3. Run $ source venv/bin/activate && cd test_project && python manage.py migrate.
  4. Create admin user, run $ python manage.py createsuperuser --email admin@mail.com --username admin.
  5. Run $ python manage.py runserver 0.0.0.0:8000 and when server is ready, open the admin page and logged in with the previous Admin account.

Installation

$ pip install drf-model-serializer

or add this on your requirements.txt

drf-model-serializer==0.0.1

Get involved!

We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.

Please report bugs via the github issue tracker.

Master git repository drf-model-serializer.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-model-serializer-0.0.2.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

drf_model_serializer-0.0.2-py2-none-any.whl (4.4 kB view details)

Uploaded Python 2

File details

Details for the file drf-model-serializer-0.0.2.tar.gz.

File metadata

  • Download URL: drf-model-serializer-0.0.2.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.1 requests/2.26.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/2.7.16

File hashes

Hashes for drf-model-serializer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 db18d21387ac6423aa470478db6ea0262685fe9dd6f217006c17e6f150e00f16
MD5 45c6b02fd3bf55817916cb96afff3d1b
BLAKE2b-256 fc0615f30262a2e6afc84e6043ca7bcbc60f511fbab28c1db92b22fcbb78d8d6

See more details on using hashes here.

File details

Details for the file drf_model_serializer-0.0.2-py2-none-any.whl.

File metadata

  • Download URL: drf_model_serializer-0.0.2-py2-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.7.1 requests/2.26.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/2.7.16

File hashes

Hashes for drf_model_serializer-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 0a1d8fba93dec1e26f5a8c3380bb572d41c5e387535341268b62ace9f487f5a6
MD5 6201d6b1f8558668129867306a8b881c
BLAKE2b-256 6fb48579c16adae885f8c9475436736e9f7534dbca97b8bf555598ae7a8ae3bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page