drf-extra-fields provides additional serializer fields for Django Rest Framework, extending its capabilities beyond the default offerings. It includes fields for handling base64 encoded files, image dimensions, and PostgreSQL range types, among others. The library is actively maintained with irregular minor releases, ensuring compatibility with recent Django and DRF versions.
pip install drf-extra-fieldsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a serializer using `Base64ImageField` and how to pass base64 encoded image data to it for validation. The `Base64ImageField` expects the base64 string without the 'data:image/...' prefix. Upon successful validation, the field returns a `SimpleUploadedFile` instance, mimicking a file upload from a standard HTML form.
Upgrade your Python environment to 3.7 or newer.
Upgrade your Django project to Django 3.2 or newer.
Update your code to expect a `SimpleUploadedFile` instance when working with the validated file object from these fields. `SimpleUploadedFile` offers similar functionality but is a different class.
Review custom image type validation logic, especially for `Base64ImageField`, and adapt it if necessary to `filetype`'s detection methods or be aware of potential changes in detected types.
Verify your `psycopg` driver installation, especially if encountering issues with `PostgreSQLRangeField` after upgrading. Consider explicitly installing `psycopg` (e.g., `psycopg[binary]`) if you need its features, or ensure `psycopg2-binary` is installed if you prefer the older driver and `psycopg` isn't present.
Install `Pillow` alongside `drf-extra-fields` using `pip install "drf-extra-fields[image]"`.
Ensure you are using PostgreSQL as your database backend and install a compatible `psycopg` driver using `pip install "drf-extra-fields[postgres]"`. Make sure your PostgreSQL version supports the required range functions.
Check the `ALLOWED_TYPES` property on your `Base64ImageField`. You can extend it by passing `allowed_types` to the field definition in your serializer, e.g., `Base64ImageField(allowed_types=['jpeg', 'png', 'webp'])`.
Correct the import statement to `from drf_extra_fields.fields import Base64ImageField` (or other fields as needed).