Coder Social home page Coder Social logo

django-rf-candv-choices's Introduction

django-rf-candv-choices

Version of PyPI package Number of downloads of PyPI package Supported versions of Python Package license

Use django-candv-choices with django-rest-framework.

Table of contents

Installation

Install from PyPI:

$ pip install django-rf-candv-choices

Usage

Serializing choices

For example, you have some model which uses candv_x.django.choices.ChoiceField to store one of allowed values. This might be a User model, which has user_role field, where values for user_role are defined by USER_ROLES constants container.

Use candv_x.rest_framework.choices.ChoiceField for user_role serialization:

from rest_framework import serializers
from candv_x.rest_framework.choices import ChoiceField

from .constants import USER_ROLES
from .models import User


class UserSerializer(serializers.ModelSerializer):
    user_role = ChoiceField(USER_ROLES)

    class Meta:
        model = User
        fields = ('id', 'name', 'user_role', )

Exposing choices to the outer world

If you need to expose list of possible choices for your field, then you can use a ChoicesList base view to get a class-based view:

from candv_x.rest_framework.choices import ChoicesList

from .constants import USER_ROLES


class UserRoleList(ChoicesList):
    choices = USER_ROLES

The only thing you need to do is to define choices attribute.

There is a faster way also: you can use a coalitions_list view factory to get a function-based view:

from candv_x.rest_framework.choices import simple_choices_list

from .constants import USER_ROLES


user_role_list = simple_choices_list(USER_ROLES)

Changelog

You can click a version name to see a diff with the previous one.

  • 1.0.0 (Aug 1, 2015)

    Initial version

django-rf-candv-choices's People

Contributors

o3bvv avatar

Stargazers

 avatar

Watchers

 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.