GithubHelp home page GithubHelp logo

gnrfan / django-sequence-field Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 21.0 216 KB

A Django Field for creating templated sequence strings

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

django-sequence-field's Introduction

Django Sequence Field

A Django field for creating templated sequenced strings.

Usage:

Importing and using the SequenceField class:

from sequence_field.fields import SequenceField

# Create your models here.

class TestModel(models.Model):

    sequence = SequenceField(
        key='test.sequence.1',
        template='%Y%m%d%(code)s%NNNNN',
        params={'code':'ABC'},
        auto=True
    )
 

When creating new objects, the sequence is generated automatically based on the template:

from my_app.models import TestModel

obj = TestModel()
obj.save()

print obj.sequence # 20140703ABC00001

obj = TestModel()
obj.save()

print obj.sequence # 20140703ABC00002

An accompaning Sequence model is used by this app to store the current value for each key. When a new key is declared in a field a new Sequence instance is created and saved on the fly.

If you need to customize the creation of each sequence value passing different parameters you'll have to do it at the model level with code like this:

from sequence_field.models import Sequence
print Sequence.next(
  'test.sequence.1', template='%Y%m%d%(code)s%NNNNN', params={'code':'XYZ'}
) # 20140703XYZ00003

Templates can also be stored in the database so you don't have to pass them all the time. If a template is provided the first time a key is used it gets automatically stored.

>>> from sequence_field.models import Sequence
>>> Sequence.next('test.sequence.20', template='%m%d%Y-%NNNNNNNNNNN')
'07042014-00000000001'
>>> Sequence.next('test.sequence.20')
'07042014-00000000002'

You can use the provided Expander classes or build your own. As an example see the code for the TimeExpander class the uses Python's strftime function to perform time-related expansions:

class TimeExpander(BaseExpander):                                               
                                                                                
    def expand(self, template=None, count=None, params={}, value=None):         
        import time                                                             
        (template, count, params, value) = self.setvars(                        
            template, count, params, value                                      
        )                                                                       
        return time.strftime(value)

The default expanders are: (taken from the constants.py file)

SEQUENCE_FIELD_DEFAULT_EXPANDERS = (                                            
    'sequence_field.expanders.NumericExpander',                                 
    'sequence_field.expanders.TimeExpander',                                    
    'sequence_field.expanders.ParameterExpander',                               
) 

At your Django project's settings.py file you can customize this variables: (default values are shown)

SEQUENCE_FIELD_DEFAULT_VALUE # 1

SEQUENCE_FIELD_ADMIN # True

SEQUENCE_FIELD_DEFAULT_TEMPLATE # '%N'

SEQUENCE_FIELD_DEFAULT_PATTERN #  r'(\d+)'

SEQUENCE_FIELD_DEFAULT_EXPANDERS # Already mentioned in the previous section.

Installation from Github

pip install https://github.com/gnrfan/django-sequence-field/zipball/master

Add this line to your requirements.txt file:

-e git+https://github.com/gnrfan/django-sequence-field.git@HEAD#egg=django-sequence-field

Then just run:

pip install -r requirements.txt.

(c) 2014 Antonio Ognio [email protected]

django-sequence-field's People

Contributors

gnrfan avatar

Stargazers

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

Watchers

 avatar  avatar

django-sequence-field's Issues

appregistryerror with django 1.7

django 1.7 documentation states to use ugettext_lazy; however, changing this in strings.py doesn't help.

django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

This should be easily reproducible with any model using SequenceField and Django 1.7.

Django sequence field is creating two instances of SequenceField

I am having a weird error, i installed the package, setup the settings, run the migrate, in one of my models i created a field:

...

class Order(models.Model):
    number = SequenceField(
        key='order.sequence.1', template='%b%NNNN', auto=True)

When i ran the makemigrations i get the following error: SequenceFieldException: The key parameter is mandatory and is missing. Digging into that i put an pdb on the SequenceField __init__ function. The first time the pdb was called it is on my number field, but there is someone else instantiating this Field and in the next time i am getting that error.

I am using the django 1.8.4

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.