Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
sorl committed May 20, 2011
0 parents commit e03710e
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
@@ -0,0 +1,15 @@
*.pyc
.DS_Store
._*
.~*#
*.swp
*.swo
*.swn
.svn
*.orig
_build
build
dist
*.egg-info
docs/_build

30 changes: 30 additions & 0 deletions 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.
1 change: 1 addition & 0 deletions MANIFEST.in
@@ -0,0 +1 @@
include LICENSE README.rst
42 changes: 42 additions & 0 deletions 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()

26 changes: 26 additions & 0 deletions 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',
],
)

1 change: 1 addition & 0 deletions stringfield/__init__.py
@@ -0,0 +1 @@
from stringfield.base import StringField
40 changes: 40 additions & 0 deletions 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

0 comments on commit e03710e

Please sign in to comment.