Skip to main content

Metadata generator for Django Rest Framework

Project description


pip install -e git+


class Author(models.Model):
    name = models.CharField(max_length=255)
    birth = models.DateField('birth date')

    def __str__(self):

class Publisher(models.Model):
    name = models.CharField(max_length=255)
    state = models.PositiveSmallIntegerField(
        'publisher state',
            (0, 'Active'),
            (1, 'Disabled')

    def __str__(self):

class Book(models.Model):
    title = models.CharField(max_length=255)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)


from drf_metadata.meta import MetaData, AbstractField, CustomMetadata

class BookMetadata(MetaData):
    model = Book

    serializers = {
        'publisher': PublisherSerializer

    update_fields = {
        'authors': {
            'omg': {'lol': 1}

    # skip serialization
    dataset_urls = {
        'authors': '/author/',
        'publisher': '/publisher/',

    def get_authors_queryset(self, field):
        return Author.objects.filter(name='author0')

    def get_authors_serializer(self, field):
        # self.request, self.view, self.obj are available in MetaData instance
        return AuthorSerializer

Usage with django-rest-framework

# or redefine OPTIONS handler
class BookViewSet(viewsets.ReadOnlyModelViewSet):
    def describe_book(self, request):
        md = metadata.BookMetadata().determine_metadata(request, self)
        return Response(md)

Sample response

    "title": "book", "description": "description",
    "fields": [
            "type": "CharField", "max_length": 255,
            "name": "title",
            "verbose_name": "title",
            "help_text": "", "blank": false, "null": false, "editable": true, "required": true},
            "type": "ForeignKey",
            "name": "publisher", "verbose_name": "publisher", "help_text": "",
            "blank": false, "null": false, "editable": true, "required": true,
            "data": [
                {"id": 47, "name": "pub0"},
                {"id": 48, "name": "pub1"},
                {"id": 49, "name": "pub2"}
            "type": "ManyToManyField",
            "name": "authors", "verbose_name": "authors", "help_text": "",
            "blank": false, "null": false, "editable": true, "required": true,
            "data": [
                {"id": 37, "name": "author0"},
                {"id": 38, "name": "author1"},
                {"id": 39, "name": "author2"}

Non-model MetaData

class ImpersonateMetadata(CustomMetadata):
    title = 'View site as another user'
    action_name = 'Impersonate'

    fields = (

    # method fields
    def get_superfield(self, request):
        return {
            'name': 'hero',
            'super': True

    def get_lol(self, request):
        return {
            'name': 'lol',
            'super': False

    # update field `user_id`
    def get_field_user_id(self, field_name, request):
        return {
            'lol': 1

Project details

Download files

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

Files for drf-metadata, version 0.1.11
Filename, size File type Python version Upload date Hashes
Filename, size drf_metadata-0.1.11-py3-none-any.whl (13.8 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size drf-metadata-0.1.11.tar.gz (9.4 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page