diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..555a866 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.pyc +.DS_Store +._* +.~*# +*.swp +*.swo +*.swn +.svn +*.orig +_build +build +dist +*.egg-info +docs/_build + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fdfe9d8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,30 @@ +django-stringfield +================== + +Copyright (c) 2011, Aino +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + * Neither the name of the sorl-thumbnail the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..8dce522 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include LICENSE README.rst diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..97440db --- /dev/null +++ b/README.rst @@ -0,0 +1,42 @@ + +django-stringfield +================== + +A string field that tries not to enforce length on database level. Currently +implemented as: + +PostgreSQL + ``character varying`` + +MySQL + ``VARCHAR (65528)`` + MySQL >= 5.0.3 should be able to handle a maximum length of 65535 but that + does not work in my empirical testing using mysql 5.1.41 where 65528 is the + maximum considering ``NULL`` and ``NOT NULL``. + +SQLite & Other backends + ``TEXT`` + + +Installation +------------ +:: + + pip install django-stringfield + + +Usage +----- +You use this just like the normal ``django.db.models.CharField`` except that the +key word argument ``max_length`` works a little differently: + +* It is optional and defaults to 255 +* It only enforces max length on the default formfield **not** on the database. + +Example usage:: + # models.py + from stringfield import StringField + + class MyModel(models.Model): + name = StringField() + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e7120c3 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +from setuptools import setup, find_packages + + +setup( + name='django-stringfield', + version='0.1', + description='Better string field for Django.', + long_description=open('README.rst').read(), + author='Mikko Hellsing', + author_email='mikko@aino.se', + license='BSD', + url='https://github.com/aino/django-stringfield', + packages=find_packages(), + zip_safe=False, + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Environment :: Web Environment', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: BSD License', + 'Operating System :: OS Independent', + 'Programming Language :: Python', + 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', + 'Framework :: Django', + ], +) + diff --git a/stringfield/__init__.py b/stringfield/__init__.py new file mode 100644 index 0000000..27219d7 --- /dev/null +++ b/stringfield/__init__.py @@ -0,0 +1 @@ +from stringfield.base import StringField diff --git a/stringfield/base.py b/stringfield/base.py new file mode 100644 index 0000000..5f39d30 --- /dev/null +++ b/stringfield/base.py @@ -0,0 +1,40 @@ +from django.db import models +from django.utils.translation import ugettext_lazy as _ + + +class StringField(models.CharField): + description = _("String") + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = kwargs.get('max_length', 255) + super(models.CharField, self).__init__(*args, **kwargs) + + def get_internal_type(self): + return "TextField" + + def to_python(self, value): + if isinstance(value, basestring) or value is None: + return value + return smart_unicode(value) + + def get_prep_value(self, value): + return self.to_python(value) + + def formfield(self, **kwargs): + defaults = {'max_length': self.max_length} + defaults.update(kwargs) + return super(CharField, self).formfield(**defaults) + + def db_type(self, connection): + if connection.vendor == 'postgresql': + return 'character varying' + if connection.vendor == 'mysql': + return 'VARCHAR (65528)' + return 'TEXT' + + def south_field_triple(self): + from south.modelsinspector import introspector + name = '%s.%s' % (self.__class__.__module__ , self.__class__.__name__) + args, kwargs = introspector(self) + return name, args, kwargs +