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 (don't include model instances to choice field)
    dataset_urls = {
        'authors': '/author/',
        'publisher': '/publisher/',

    # custom queryset for instance (self has `obj` if you need to filter qs)
    def get_authors_queryset(self, field):
        return Author.objects.filter(name='author0')

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

    # update field bundle with runtime values
    def update_authors_field_meta(field, obj):
        return {'new': 1, 'obj': str(obj)}

    # use own serializer
    def get_publisher_field_meta(field, obj):
        return {'new': 1, 'obj': str(obj)}

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.

Source Distribution

drf-metadata-0.1.12.tar.gz (10.0 kB view hashes)

Uploaded Source

Built Distribution

drf_metadata-0.1.12-py3-none-any.whl (14.4 kB view hashes)

Uploaded Python 3

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