Coder Social home page Coder Social logo

smartchoices's Introduction

Smart Choices

A better choices interface for Django apps

A common Django pattern is to create this complicated tuple list of tuples that represent key/value pairs. The utilities here help make that a bit easier.

The basics

To use, implement a Choices class like this:

    class KennyRogers(smartchoices.Choices):
        HOLD_EM = smartchoices.Choice()
        FOLD_EM = smartchoices.Choice()
        WALK_AWAY = smartchoices.Choice()
        RUN = smartchoices.Choice()

Then reference it in a model choices field like this:

gambler = models.IntegerField(choices=KennyRogers.choices)

All values provided from a smartchoices Choices object will be integers, so the Django field needs to be an IntegerField. This is smarter for your database, too.

Smart names

Optionally, if you'd like to expose nicer looking names for the choices, you can specify them in the Choice creation, like so:

    Choice(name="The Quick Brown Fox")

Or you may turn on smart_names in the Meta class of the Choices object like this:

    class TypingClass(smartchoices.Choices):
        class Meta:
            smart_names = True
        THE_QUICK_BROWN_FOX = smartchoices.CHOICE()

Set values like an enum

Just like enums, you may also choose to manually set the value of a choice.

    class JayZProblems(smartchoices.Choices):
        FOES_THAT_WANNA_MAKE_SURE_MY_CASKETS_CLOSED = smartchoices.Choice()
        YOUNG = smartchoices.Choice()
        BLACK = smartchoices.Choice()
        HATS_REAL_LOW = smartchoices.Choice()

        THE_B_WORD = smartchoices.Choice(100)
        MONEY = smartchoices.Choice()

Just like an enum, the counting starts 0, 1, 2, 3 and then jumps to 100, and continues on with a value of 101 for MONEY.

smartchoices's People

Contributors

rockstar avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

mblayman

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.