Code Monkey home page Code Monkey logo

djangorestframework-expander's Introduction

djangorestframework-expander

Build Status

A serializer mixin for Django REST Framework to expand object representations inline

Requirements

  • Python (2.7, 3.2, 3.3, 3.4, 3.5)
  • Django (1.7, 1.8, 1.9)
  • Django REST Framework (3.1, 3.2, 3.3)

Installation

Install using pip

pip install djangorestframework-expander

Quick Start

  • Must inherit from ExpanderSerializerMixin
  • Expandable fields are defined using the expandable_fields attribute on the serializer Meta class.
    • Key is the field name.
    • Value is the serializer class - e.g. MenuItemSerializer OR
    • Value is a 3 tuple of (class, args, kwargs) - e.g. (MenuItemSerializer, (), {'many': True})
  • NOTE field does not have to appear in fields to appear in expandable_fields.
  • Expansion is controlled using the expand URL paramter. e.g. /menu-items/?expand=menu
  • Expand multiple fields with a comma. e.g. /menu-items/?expand=menu,price_bracket
  • Expand nested fields by using a dot. e.g. /menu-items/?expand=menu.restaurant

Example

from expander import ExpanderSerializerMixin


class RestaurantSerializer(ExpanderSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = Restaurant
        fields = ('id', 'title')


class MenuSerializer(ExpanderSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = Menu
        fields = ('id', 'restaurant', 'title')
        expandable_fields = {
            'restaurant': RestaurantSerializer,
        }

class PriceBracketSerializer(serializers.ModelSerializer):
    class Meta:
        model = PriceBracket
        fields = ('id', 'title')


class MenuItemSerializer(ExpanderSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = MenuItem
        fields = ('id', 'menu', 'title', 'description', 'price', 'price_bracket')
        expandable_fields = {
            'menu': MenuSerializer,
            'price_bracket': PriceBracketSerializer,
        }

Making a request for a menu item returns the usual results to start with.

GET /menu-items/1/

{
    "id": 1,
    "menu": 3,
    "title": "Breakfast",
    "description": "",
    "price": "3.50"
}

Expand menu

GET /menu-items/1/?expand=menu

{
    "id": 1,
    "menu": {
        "id": 3,
        "restaurant": 6,
        "title": "Breakfast"
    },
    "title": "Eggs",
    "description": "",
    "price": "3.50"
}

Expand nested

GET /menu-items/1/?expand=menu.restaurant

{
    "id": 1,
    "menu": {
        "id": 3,
        "restaurant": {
            "id": 6,
            "title": "Bob's Bed and Breakfast"
        },
        "title": "Breakfast"
    },
    "title": "Eggs",
    "description": "",
    "price": "3.50"
}

djangorestframework-expander's People

Contributors

ryanpineo avatar

Watchers

Ray avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.