All Projects → marshmallow-code → Django Rest Marshmallow

marshmallow-code / Django Rest Marshmallow

Licence: isc
Marshmallow schemas for Django REST framework

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Rest Marshmallow

Marshmallow Jsonapi
JSON API 1.0 (https://jsonapi.org/) formatting with marshmallow
Stars: ✭ 203 (+2.53%)
Mutual labels:  rest-api, marshmallow, serialization
Drf Cheat Sheet
Cheat sheet / quick reference guide for Django REST Framework.
Stars: ✭ 99 (-50%)
Mutual labels:  rest-api, django
Mezzanine Api
RESTful web API for Mezzanine CMS
Stars: ✭ 84 (-57.58%)
Mutual labels:  rest-api, django
Drf Api Tracking
Fork of aschn/drf-tracking so that we can maintain and release newer versions
Stars: ✭ 117 (-40.91%)
Mutual labels:  rest-api, django
The Complete Guide To Drf And Vuejs
📢 Source Code from my Web Dev Course *The Complete Guide To Django REST Framework and Vue JS* (Lang: English & Italian)
Stars: ✭ 78 (-60.61%)
Mutual labels:  rest-api, django
Flask Restplus Server Example
Real-life RESTful server example on Flask-RESTplus
Stars: ✭ 1,240 (+526.26%)
Mutual labels:  rest-api, marshmallow
Schema Registry
Confluent Schema Registry for Kafka
Stars: ✭ 1,647 (+731.82%)
Mutual labels:  rest-api, schema
Cookiecutter Django Rest
Build best practiced apis fast with Python3
Stars: ✭ 1,108 (+459.6%)
Mutual labels:  rest-api, django
Django Salesman
Headless e-commerce framework for Django.
Stars: ✭ 157 (-20.71%)
Mutual labels:  rest-api, django
Flama
🔥 Fire up your API with this flamethrower
Stars: ✭ 161 (-18.69%)
Mutual labels:  marshmallow, schema
Rest Api Basics
This is a basic guide on how to build a REST API with Django & Python. For much deeper depth, check out our new course on REST API: (https://kirr.co/90kxtx)
Stars: ✭ 171 (-13.64%)
Mutual labels:  rest-api, django
Django Vue Template
Django Rest + Vue JS Template
Stars: ✭ 1,155 (+483.33%)
Mutual labels:  rest-api, django
Webargs
A friendly library for parsing HTTP request arguments, with built-in support for popular web frameworks, including Flask, Django, Bottle, Tornado, Pyramid, webapp2, Falcon, and aiohttp.
Stars: ✭ 1,145 (+478.28%)
Mutual labels:  marshmallow, django
Drf Autodocs
Ultimately automated DRF documentation rendering(UNMAINTAINED)
Stars: ✭ 82 (-58.59%)
Mutual labels:  rest-api, django
Best Of Web Python
🏆 A ranked list of awesome python libraries for web development. Updated weekly.
Stars: ✭ 1,118 (+464.65%)
Mutual labels:  rest-api, django
Wq.db
☁🌐 wq's db library, extending Django REST framework to support apps for geospatial field data collection, citizen science, and crowdsourcing.
Stars: ✭ 101 (-48.99%)
Mutual labels:  rest-api, django
Mygpo
The gpodder.net webservice
Stars: ✭ 176 (-11.11%)
Mutual labels:  rest-api, django
Django Rest Pandas
📊📈 Serves up Pandas dataframes via the Django REST Framework for use in client-side (i.e. d3.js) visualizations and offline analysis (e.g. Excel)
Stars: ✭ 1,030 (+420.2%)
Mutual labels:  rest-api, django
Beeschema
Binary Schema Library for C#
Stars: ✭ 46 (-76.77%)
Mutual labels:  schema, serialization
Awesome Python Models
A curated list of awesome Python libraries, which implement models, schemas, serializers/deserializers, ODM's/ORM's, Active Records or similar patterns.
Stars: ✭ 124 (-37.37%)
Mutual labels:  schema, serialization

django-rest-marshmallow

Marshmallow schemas for Django REST framework.


Overview

django-rest-marshmallow provides an alternative serializer implementation to the built-in serializers, by using the python marshmallow library, but exposing the same API as REST framework's Serializer class.

Requirements

  • Python (3.6+)
  • Django REST framework (3.8+)
  • Marshmallow (3.0.0+)

Installation

Install using pip...

$ pip install django-rest-marshmallow

Usage

Define your schemas as you would with marshmallow, but importing the Schema class from rest_marshmallow instead.

from rest_marshmallow import Schema, fields

class CustomerSchema(Schema):
    name = fields.String()
    email = fields.Email()
    created_at = fields.DateTime()

The Schema class has the same interface as a Django REST framework serializer, so you can use it in your generic views...

class CustomerListView(generics.ListAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSchema

Or use the serializer API directly, for either serialization...

serializer = CustomerSchema(queryset, many=True)
return Response(serializer.data)

Or for validation...

serializer = CustomerSchema(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.validated_data

Instance create and update

If you want to support serializer.save() you'll need to define the .create() and/or .update() methods explicitly.

class CustomerSchema(Schema):
    name = fields.String()
    email = fields.Email()
    created_at = fields.DateTime()

    def create(self, validated_data):
        return Customer.objects.create(**validated_data)

    def update(self, instance, validated_data):
        for key, value in validated_data.items():
            setattr(instance, key, value)
        instance.save()
        return instance

You can now use .save() from your view code…

serializer = CustomerSchema(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

Or use the schema together with generic views that create or update instances...

class CustomerListView(generics.ListCreateAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSchema

Note that you should always use the create() and update() methods instead of overriding the make_object() marshmallow method.

Nested representations

For nested representations, use marshmallow's standard Nested field as usual.

from rest_marshmallow import fields, Schema

class ArtistSchema(Schema):
    name = fields.String()

class AlbumSchema(Schema):
    title = fields.String()
    release_date = fields.Date()
    artist = fields.Nested(ArtistSchema)

Excluding fields

The marshmallow only and exclude arguments are also valid as serializer arguments:

serializer = CustomerSchema(queryset, many=True, only=('name', 'email'))
return Response(serializer.data)

Testing

Install testing requirements.

$ pip install -r requirements.txt

Run with runtests.

$ ./runtests.py

You can also use the excellent tox testing tool to run the tests against all supported versions of Python and Django. Install tox globally, and then simply run:

$ tox

Documentation

To build the documentation, you'll need to install mkdocs.

$ pip install mkdocs

To preview the documentation:

$ mkdocs serve
Running at: http://127.0.0.1:8000/

To build the documentation:

$ mkdocs build
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].