Giter Site home page Giter Site logo

django-choice-object's Introduction

Django choice object

Build Status

I am a choice object for Django. I make using choices in forms and models easier.

Deprecated: This is not needed since Django 3.0 which includes enumeration types which work similarly.

Install

pip install django-choice-object

Usage

Choice classes inherit from Choice. Each attribute can have one or two values, the first being the value to be stored in the database and the second optional one being the display text. If no display text is given then it is created from the attribute name.

Example

from django_choice_object import Choice
from django.db import models
    
class SomeChoice(Choice):
    FIRST_CHOICE = 1
    SECOND_CHOICE = 2
    THIRD_CHOICE = 3
    LAST_CHOICE = 4, "I am the display text"
    
class SomeModel(models.Model):
    some_field = models.IntegerField(choices=SomeChoice)
    
>>> list(SomeChoice)
[(1, 'First Choice'), (2, 'Second Choice'), (3, 'Third Choice'), (4, 'I am the display text')]
>>> SomeModel.objects.filter(some_field=SomeChoice.FIRST_CHOICE).all()
[object list...]
>>> SomeModel.objects.get(id=1).some_field
1
>>> SomeModel().some_field = SomeChoice.LAST_CHOICE

Advanced stuff

Grouping

Grouping choice values is a fairly common need. You can add groups of values to a choice like so:

class ChoiceWitHGroups(Choice):
    FIRST = 1
    SECOND = 2
    THIRD = 3
    FOURTH = 4
    
    START_GROUP = {FIRST, SECOND}
    END_GROUP = {THIRD, FOURTH}

Any value that ends with _GROUP and is a set will be ignored as a choice.

Ordering

Choices can be ordered in three ways. The default is by the value, but this can be configured using the _order_by attribute:

class TestChoiceOrdered(Choice):
    LAST = 1, "zzzzzzzzz"
    FIRST = 2, "aaaaaaaa"
    _order_by = "name"

In the code snippet above the FIRST attribute would appear first as they are sorted by the name rather than the value.

You can also specify a custom sorting key:

class WeirdChoice(Choice):
    LAST = 'last', 'Last', 10
    FIRST = 'first', 'First', 1

    @staticmethod
    def _get_sort_key(value):
        return value[2]

In this case the third item in the tuple would be the item to sort on.

Contributing

  • Set up a virtualenv: virtualenv .
  • Install the requirements: pip install -r requirements.txt
  • Run the tests: nosetests .

django-choice-object's People

Contributors

adamchainz avatar michael-k avatar mjtamlyn avatar orf 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.