Skip to content

Commit ab3f2e3

Browse files
committed
Issue #22: Fields storage of model is dict instead of tuple.
1 parent baca2db commit ab3f2e3

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

domain_models/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def bind_model_cls(self, model_cls):
3737
'could not be rebound to "{2}"'.format(
3838
self, self.model_cls, model_cls))
3939
self.model_cls = model_cls
40-
return self
40+
return self.name, self
4141

4242
def init_model(self, model, value):
4343
"""Init model with field."""

domain_models/models.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def prepare_fields_attribute(attribute_name, attributes, class_name):
6363
@staticmethod
6464
def bind_fields_to_model_cls(cls, model_fields):
6565
"""Bind fields to model class."""
66-
return tuple(field.bind_model_cls(cls)
67-
for field in model_fields)
66+
return dict(field.bind_model_cls(cls) for field in model_fields)
6867

6968
@staticmethod
7069
def bind_collection_to_model_cls(cls):
@@ -111,15 +110,15 @@ class DomainModel(object):
111110

112111
Collection = collections.Collection
113112

114-
__fields__ = tuple()
113+
__fields__ = dict()
115114
__view_key__ = tuple()
116115
__unique_key__ = tuple()
117116
__slots_optimization__ = True
118117

119118
def __init__(self, **kwargs):
120119
"""Initializer."""
121-
for field in self.__class__.__fields__:
122-
field.init_model(self, kwargs.get(field.name))
120+
for name, field in six.iteritems(self.__class__.__fields__):
121+
field.init_model(self, kwargs.get(name))
123122
super(DomainModel, self).__init__()
124123

125124
def __eq__(self, other):
@@ -164,14 +163,16 @@ def __repr__(self):
164163
"""Return Pythonic representation of domain model."""
165164
return '{module}.{cls}({fields_values})'.format(
166165
module=self.__class__.__module__, cls=self.__class__.__name__,
167-
fields_values=', '.join('='.join((field.name,
168-
repr(field.get_value(self))))
169-
for field in self.__class__.__fields__))
166+
fields_values=', '.join(
167+
'='.join((name, repr(field.get_value(self))))
168+
for name, field in
169+
six.iteritems(self.__class__.__fields__)))
170170

171171
def __str__(self):
172172
"""Return string representation of domain model."""
173173
if not self.__class__.__view_key__:
174174
return self.__repr__()
175+
175176
return '{module}.{cls}({fields_values})'.format(
176177
module=self.__class__.__module__, cls=self.__class__.__name__,
177178
fields_values=', '.join('='.join((field.name,
@@ -181,8 +182,9 @@ def __str__(self):
181182
@property
182183
def __data__(self):
183184
"""Read only dictionary of model fields/values."""
184-
return dict((field.name, field.get_value(self))
185-
for field in self.__class__.__fields__)
185+
return dict((name, field.get_value(self))
186+
for name, field in
187+
six.iteritems(self.__class__.__fields__))
186188

187189
def get(self, field_name, default=None):
188190
"""Return the value of the field.
@@ -204,9 +206,8 @@ def get(self, field_name, default=None):
204206
"Model doesn't have a field '{name}'".format(name=field_name))
205207

206208
if default is not None:
207-
field = dict((field.name, field) for field in
208-
self.__class__.__fields__).get(field_name)
209209
try:
210+
field = self.__class__.__fields__[field_name]
210211
default = field.converter(default)
211212
except (TypeError, ValueError):
212213
raise AttributeError(

0 commit comments

Comments
 (0)