Permalink
Browse files

Move attributes back into field and just use column for moving to/from

python and database
  • Loading branch information...
1 parent 6e92c3d commit 007075c141d45375e33ff273a3abb687d6c2468a @coleifer committed Mar 12, 2012
Showing with 24 additions and 29 deletions.
  1. +24 −29 peewee.py
View
@@ -1876,13 +1876,6 @@ class Column(object):
db_field = ''
template = '%(column_type)s'
- def __init__(self, **attributes):
- self.attributes = self.get_attributes()
- self.attributes.update(**attributes)
-
- def get_attributes(self):
- return {}
-
def python_value(self, value):
return value
@@ -1891,20 +1884,14 @@ def db_value(self, value):
def render(self, db):
params = {'column_type': db.column_for_field_type(self.db_field)}
- params.update(self.attributes)
return self.template % params
class VarCharColumn(Column):
db_field = 'string'
- template = '%(column_type)s(%(max_length)d)'
-
- def get_attributes(self):
- return {'max_length': 255}
def db_value(self, value):
- value = value or ''
- return value[:self.attributes['max_length']]
+ return value or ''
class TextColumn(Column):
@@ -1966,13 +1953,6 @@ class DoubleColumn(FloatColumn):
class DecimalColumn(Column):
db_field = 'decimal'
- field_template = '%(column_type)s(%(max_digits)d, %(decimal_places)d)'
-
- def get_attributes(self):
- return {
- 'max_digits': 10,
- 'decimal_places': 5,
- }
def db_value(self, value):
return value or decimal.Decimal(0)
@@ -2022,10 +2002,14 @@ def __init__(self, null=False, db_index=False, unique=False, verbose_name=None,
self.db_column = db_column
self.default = default
- self.attributes = kwargs
+ self.attributes = self.get_attributes()
+ self.attributes.update(kwargs)
Field._field_counter += 1
self._order = Field._field_counter
+
+ def get_attributes(self):
+ return {}
def add_to_class(self, klass, name):
self.name = name
@@ -2035,16 +2019,19 @@ def add_to_class(self, klass, name):
self.column = self.get_column()
setattr(klass, name, FieldDescriptor(self))
+
+ def get_column_class(self):
+ return self.column_class
def get_column(self):
- return self.column_class(**self.attributes)
+ return self.get_column_class()()
def render_field_template(self):
params = {
'column': self.column.render(self.model._meta.database),
'nullable': ternary(self.null, '', ' NOT NULL'),
}
- params.update(self.column.attributes)
+ params.update(self.attributes)
return self.field_template % params
def db_value(self, value):
@@ -2063,7 +2050,11 @@ def class_prepared(self):
class CharField(Field):
+ field_template = "%(column)s(%(max_length)s)%(nullable)s"
column_class = VarCharColumn
+
+ def get_attributes(self):
+ return {'max_length': 255}
class TextField(Field):
@@ -2095,8 +2086,15 @@ class DoubleField(Field):
class DecimalField(Field):
+ field_template = '%(column)s(%(max_digits)d, %(decimal_places)d)%(nullable)s'
column_class = DecimalColumn
+ def get_attributes(self):
+ return {
+ 'max_digits': 10,
+ 'decimal_places': 5,
+ }
+
class PrimaryKeyField(IntegerField):
column_class = PrimaryKeyColumn
@@ -2119,9 +2117,6 @@ def get_column_class(self):
if self.model._meta.pk_sequence != None and self.model._meta.database.adapter.sequence_support:
self.column_class = PrimaryKeySequenceColumn
return self.column_class
-
- def get_column(self):
- return self.get_column_class()(**self.attributes)
class ForeignRelatedObject(object):
@@ -2230,7 +2225,7 @@ def get_column(self):
to_col_class = to_pk.get_column_class()
if to_col_class not in (PrimaryKeyColumn, PrimaryKeySequenceColumn):
self.column_class = to_pk.get_column_class()
- return self.column_class(**self.attributes)
+ return self.column_class()
def class_prepared(self):
# unfortunately because we may not know the primary key field
@@ -2376,7 +2371,7 @@ def __new__(cls, name, bases, attrs):
pk_field = _meta.fields[_meta.pk_name]
pk_col = pk_field.column
if _meta.pk_sequence and _meta.database.adapter.sequence_support:
- pk_col.attributes['nextval'] = " default nextval('%s')" % _meta.pk_sequence
+ pk_field.attributes['nextval'] = " default nextval('%s')" % _meta.pk_sequence
_meta.auto_increment = isinstance(pk_col, PrimaryKeyColumn)

0 comments on commit 007075c

Please sign in to comment.