{"id":7192,"library":"drf-extra-fields","title":"DRF Extra Fields","description":"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.","status":"active","version":"3.7.0","language":"en","source_language":"en","source_url":"https://github.com/Hipo/drf-extra-fields","tags":["django","drf","rest framework","serialization","fields","base64","image","postgres"],"install":[{"cmd":"pip install drf-extra-fields","lang":"bash","label":"Basic installation"},{"cmd":"pip install \"drf-extra-fields[image]\"","lang":"bash","label":"For Base64ImageField and related image fields (installs Pillow)"},{"cmd":"pip install \"drf-extra-fields[postgres]\"","lang":"bash","label":"For PostgreSQLRangeField (installs psycopg/psycopg2)"}],"dependencies":[{"reason":"Core dependency for any Django project.","package":"django","optional":false},{"reason":"Core dependency for Django Rest Framework serializers.","package":"djangorestframework","optional":false},{"reason":"Required for image processing fields like Base64ImageField.","package":"Pillow","optional":true},{"reason":"Required for PostgreSQL specific fields like PostgreSQLRangeField (psycopg3 preferred, fallback to psycopg2).","package":"psycopg","optional":true}],"imports":[{"note":"Common mistake to import directly from the top-level package; fields are in `drf_extra_fields.fields`.","wrong":"from drf_extra_fields import Base64ImageField","symbol":"Base64ImageField","correct":"from drf_extra_fields.fields import Base64ImageField"},{"symbol":"Base64FileField","correct":"from drf_extra_fields.fields import Base64FileField"},{"symbol":"PostgreSQLRangeField","correct":"from drf_extra_fields.fields import PostgreSQLRangeField"},{"symbol":"ColorField","correct":"from drf_extra_fields.fields import ColorField"}],"quickstart":{"code":"import base64\nfrom rest_framework import serializers\nfrom drf_extra_fields.fields import Base64ImageField\n\n# Example usage with a serializer\nclass MyImageSerializer(serializers.Serializer):\n    name = serializers.CharField(max_length=100)\n    image = Base64ImageField(required=True)\n\n# Simulate a base64 encoded image for demonstration\n# This is a 1x1 transparent GIF: data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==\n# Stripping the 'data:image/gif;base64,' prefix for the field's input\ntiny_gif_base64 = 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='\n\n# Example usage:\ndata = {\n    'name': 'My Awesome Image',\n    'image': tiny_gif_base64 # The base64 string without the prefix\n}\n\nserializer = MyImageSerializer(data=data)\nif serializer.is_valid(raise_exception=True):\n    print(\"Serializer data is valid!\")\n    print(serializer.validated_data)\n    # The 'image' field in validated_data will be an instance of SimpleUploadedFile\n    uploaded_file = serializer.validated_data['image']\n    print(f\"Uploaded file name: {uploaded_file.name}\")\n    print(f\"Uploaded file size: {uploaded_file.size} bytes\")\nelse:\n    print(\"Validation failed:\")\n    print(serializer.errors)\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.7 or newer.","message":"Support for Python 3.5 (v3.2.0) and 3.6 (v3.3.0) was dropped. Attempting to install or run drf-extra-fields on these Python versions will result in installation errors or runtime failures.","severity":"breaking","affected_versions":">=3.2.0"},{"fix":"Upgrade your Django project to Django 3.2 or newer.","message":"Support for Django 3.0 and 3.1 was dropped in version 3.4.0. Using newer drf-extra-fields with these Django versions can lead to compatibility issues or errors.","severity":"breaking","affected_versions":">=3.4.0"},{"fix":"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.","message":"In v3.1.0, the internal file class used by `Base64FileField` (and consequently `Base64ImageField`) changed from `ContentFile` to `SimpleUploadedFile`. If your code directly accessed the file object created by this field and expected a `ContentFile` instance, it may break.","severity":"breaking","affected_versions":">=3.1.0"},{"fix":"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.","message":"From v3.5.0, the `imghdr` library for image type detection was replaced by `filetype`. If you had custom image type validation that relied on `imghdr`'s behavior or specific return values, you might need to adjust it.","severity":"gotcha","affected_versions":">=3.5.0"},{"fix":"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.","message":"Version 3.7.0 introduced support for `psycopg` (v3) and now uses it automatically if available, preferring it over `psycopg2`. While not strictly breaking, ensure your PostgreSQL setup is compatible if you were relying on specific `psycopg2` behaviors or explicit installations.","severity":"gotcha","affected_versions":">=3.7.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `Pillow` alongside `drf-extra-fields` using `pip install \"drf-extra-fields[image]\"`.","cause":"`Pillow` is required for image-related fields (e.g., `Base64ImageField`) but was not installed. It is an optional dependency.","error":"ModuleNotFoundError: No module named 'Pillow'"},{"fix":"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.","cause":"`PostgreSQLRangeField` requires a PostgreSQL database backend and a compatible `psycopg` driver (e.g., `psycopg2` or `psycopg`). This error specifically suggests a PostgreSQL setup issue or missing `psycopg` driver, or an attempt to use it with a non-PostgreSQL database.","error":"django.db.utils.ProgrammingError: function array_upper(character varying[], integer) does not exist"},{"fix":"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'])`.","cause":"The uploaded image's MIME type is not in the `ALLOWED_TYPES` for `Base64ImageField`. By default, only common image types are allowed (e.g., jpeg, png, gif, webp). Before v3.5.0, WebP was not included by default.","error":"ValueError: The image type is not allowed."},{"fix":"Correct the import statement to `from drf_extra_fields.fields import Base64ImageField` (or other fields as needed).","cause":"Incorrect import path. The fields are located in the `drf_extra_fields.fields` submodule, not directly under `drf_extra_fields`.","error":"ImportError: cannot import name 'Base64ImageField' from 'drf_extra_fields'"}]}