Skip to content

Commit

Permalink
Add FixedCharField, fixes #631
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jul 5, 2015
1 parent 18bb986 commit 844200f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 3 additions & 0 deletions docs/peewee/models.rst
Expand Up @@ -120,6 +120,7 @@ Field types table
Field Type Sqlite Postgresql MySQL
=================== ================= ================= =================
``CharField`` varchar varchar varchar
``FixedCharField`` char char char
``TextField`` text text longtext
``DateTimeField`` datetime timestamp datetime
``IntegerField`` integer integer integer
Expand Down Expand Up @@ -162,6 +163,8 @@ Some fields take special parameters...
+================================+================================================+
| :py:class:`CharField` | ``max_length`` |
+--------------------------------+------------------------------------------------+
| :py:class:`FixedCharField` | ``max_length`` |
+--------------------------------+------------------------------------------------+
| :py:class:`DateTimeField` | ``formats`` |
+--------------------------------+------------------------------------------------+
| :py:class:`DateField` | ``formats`` |
Expand Down
11 changes: 11 additions & 0 deletions peewee.py
Expand Up @@ -54,6 +54,7 @@
'DoubleField',
'DQ',
'Field',
'FixedCharField',
'FloatField',
'fn',
'ForeignKeyField',
Expand Down Expand Up @@ -988,6 +989,15 @@ def get_modifiers(self):
def coerce(self, value):
return coerce_to_unicode(value or '')

class FixedCharField(CharField):
db_field = 'fixed_char'

def python_value(self, value):
value = super(FixedCharField, self).python_value(value)
if value:
value = value.strip()
return value

class TextField(Field):
db_field = 'text'

Expand Down Expand Up @@ -1340,6 +1350,7 @@ class QueryCompiler(object):
'datetime': 'DATETIME',
'decimal': 'DECIMAL',
'double': 'REAL',
'fixed_char': 'CHAR',
'float': 'REAL',
'int': 'INTEGER',
'primary_key': 'INTEGER',
Expand Down
1 change: 1 addition & 0 deletions playhouse/tests/models.py
Expand Up @@ -56,6 +56,7 @@ class NullModel(TestModel):
date_field = DateField(null=True)
time_field = TimeField(null=True)
boolean_field = BooleanField(null=True)
fixed_char_field = FixedCharField(null=True)


class UniqueModel(TestModel):
Expand Down
17 changes: 13 additions & 4 deletions playhouse/tests/test_fields.py
Expand Up @@ -23,10 +23,10 @@ class TestFieldTypes(ModelTestCase):
_t = datetime.time

_data = (
('char_field', 'text_field', 'int_field', 'float_field', 'decimal_field1', 'datetime_field', 'date_field', 'time_field'),
('c1', 't1', 1, 1.0, "1.0", _dt(2010, 1, 1), _d(2010, 1, 1), _t(1, 0)),
('c2', 't2', 2, 2.0, "2.0", _dt(2010, 1, 2), _d(2010, 1, 2), _t(2, 0)),
('c3', 't3', 3, 3.0, "3.0", _dt(2010, 1, 3), _d(2010, 1, 3), _t(3, 0)),
('char_field', 'text_field', 'int_field', 'float_field', 'decimal_field1', 'datetime_field', 'date_field', 'time_field', 'fixed_char_field'),
('c1', 't1', 1, 1.0, "1.0", _dt(2010, 1, 1), _d(2010, 1, 1), _t(1, 0), 'fc1'),
('c2', 't2', 2, 2.0, "2.0", _dt(2010, 1, 2), _d(2010, 1, 2), _t(2, 0), 'fc2'),
('c3', 't3', 3, 3.0, "3.0", _dt(2010, 1, 3), _d(2010, 1, 3), _t(3, 0), 'fc3'),
)

def setUp(self):
Expand Down Expand Up @@ -92,6 +92,15 @@ def test_charfield(self):
case_insens = NM.select(NM.char_field).where(NM.char_field ** ilike_str)
self.assertEqual([x[0] for x in case_insens.tuples()], ['Alpha', 'Bravo'])

def test_fixed_charfield(self):
NM = NullModel
nm = NM.create(fixed_char_field=4)
nm_db = NM.get(NM.id == nm.id)
self.assertEqual(nm_db.fixed_char_field, '4')

fc_vals = [obj.fixed_char_field for obj in NM.select().order_by(NM.id)]
self.assertEqual(fc_vals, ['fc1', 'fc2', 'fc3', '4'])

def test_intfield(self):
nm = NullModel.create(int_field='4')
nm_db = NullModel.get(NullModel.id==nm.id)
Expand Down

0 comments on commit 844200f

Please sign in to comment.