Giter Site home page Giter Site logo

leotop / django-separatedvaluesfield Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ulule/django-separatedvaluesfield

0.0 2.0 0.0 273 KB

Custom field for Django to separate multiple values in database with a separator and retrieve them as list

License: MIT License

Makefile 3.11% Python 96.89%

django-separatedvaluesfield's Introduction

django-separatedvaluesfield

Build Status

Alternative to CommaSeparatedIntegerField built-in field that supports MultipleChoiceField, custom separator and returns values as list.

Installation

Install package from PyPi:

pip install django-separatedvaluesfield

Or download the archive from GitHub and proceed to a manual installation:

curl -L https://github.com/thoas/django-separatedvaluesfield/tarball/master | tar zx
cd thoas-django-separatedvaluesfield
python setup.py install

Add SeparatedValuesField to your Django model:

# models.py
from django.db import models

from separatedvaluesfield.models import SeparatedValuesField

class Project(models.Model):
    name = models.CharField(max_length=150)
    languages = SeparatedValuesField(
        max_length=150,
        token=',',
        choices=(
            ('en', 'English'),
            ('fr', 'French')))

If your choices values are not strings, add the cast option with the type you want to apply on values (defaults to django.utils.six.text_type):

# models.py
from django.db import models

from separatedvaluesfield.models import SeparatedValuesField

class Project(models.Model):
    name = models.CharField(max_length=150)
    languages = SeparatedValuesField(
        max_length=150,
        cast=int,
        token=',',
        choices=(
            (1, 'English'),
            (2, 'French')))

If you are running Django <= 1.6, synchronize your database using syncdb:

python manage.py syncdb

If you are running Django >= 1.7, synchronize your database using migrate:

python manage.py migrate

The SeparatedValuesField behaves like a CharField which separates values with a token (default is ,).

This field is transformed as a MultipleChoiceField when you are creating a forms.ModelForm with your model.

Usage

>>> from myapp.models import Project
>>> project = Project(name='Project with strings', languages=['fr', 'en'])
>>> project.save() # save 'fr,en' in database for the column "languages"
>>> project.pk
1

>>> project = Project.objects.get(pk=1)
>>> project.languages
['fr', 'en']

# If you added "cast" option to the field to cast to 'int'
>>> project = Project(name='Project with integers', languages=[u'1', u'2'])
>>> project.save() # save '1,2' in database for the column "languages"
>>> project = Project.objects.get(pk=1)
>>> project.languages
[1, 2]

Contribute

  1. Fork the repository
  2. Clone your fork
  3. Create a dedicated branch (never ever work in master)
  4. Create your development environment with make dev
  5. Activate your environment with source .venv/bin/activate
  6. Make modifications
  7. Write tests and execute them with make test
  8. Be sure all test pass with tox
  9. If all tests pass, submit a pull request

Compatibility

This library is compatible with:

  • python2.6, django1.5
  • python2.6, django1.6
  • python2.7, django1.5
  • python2.7, django1.6
  • python2.7, django1.7
  • python2.7, django1.8
  • python3.3, django1.5
  • python3.3, django1.6
  • python3.3, django1.7
  • python3.3, django1.8
  • python3.4, django1.5
  • python3.4, django1.6
  • python3.4, django1.7
  • python3.4, django1.8

django-separatedvaluesfield's People

Contributors

gillesfabio avatar thoas avatar bricecarpentier avatar

Watchers

James Cloos avatar Igor 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.