Coder Social home page Coder Social 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-choice-object's Issues

Improved ordering options

Hi,

I have a use case where I want to explicitly order the options but want to maintain "slugs" for the keys. I can do something horrible where we order by the display names but they're actually objects with __str__ and __cmp__ but that's horrible. It would be awesome to be able to define something like this:

class MyChoices(ChoiceObject):
    _order_key = 2
    APPLES = ('apples', 'Apples', 1)
    BANANAS = ('bananas', 'Bananas', 0)

i.e. if _order_key is set go with that instead of _order_by. Or just being able to set _order_by in this manner instead. We would need to keep the "original" data to sort on as well as the current dict in _data.

I'm happy to provide a PR for this if you think it's a nice idea.

support for Python3

Is django-choice-object works with Python3. As I upgraded my code from Python2 to Python3 and I'm not sure about it.

Is nose necessary?

Is there any reason why we are using nose tests? Running python django_choice_object/tests.py would be just as simple, and seems like it should be possible given the __main__ block at the bottom of the file. You can't run either option at will due to the different python paths/imports. Perhaps actually just having a top level tests.py which runs with unittest and is outside of the shipped package would make most sense.

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.