Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'respect-fields-order-when-creating' into related_fields

  • Loading branch information...
commit c643d77a43815af006d86ae11dfadbaeb12edb1c 2 parents 71f1116 + d5b45fb
Stéphane Angel authored

Showing 2 changed files with 21 additions and 4 deletions. Show diff stats Hide diff stats

  1. +17 4 limpyd/model.py
  2. +4 0 tests/model.py
21 limpyd/model.py
@@ -90,6 +90,10 @@ def __new__(mcs, name, base, attrs):
90 90 if isinstance(field, HashableField):
91 91 _hashable_fields.append(field.name)
92 92
  93 + # keep the pk as first field
  94 + _fields.remove(pk_field.name)
  95 + _fields.insert(0, pk_field.name)
  96 +
93 97 # Save usefull attributes on the final model
94 98 setattr(it, "_fields", _fields)
95 99 setattr(it, "_hashable_fields", _hashable_fields)
@@ -154,8 +158,13 @@ def __init__(self, *args, **kwargs):
154 158 # (More robust than trying to manage a "pseudotransaction", as
155 159 # redis do not has "real" transactions)
156 160 # Here we do not set anything, in case one unique field fails
  161 + kwargs_pk_field_name = None
157 162 for field_name, value in kwargs.iteritems():
158   - if field_name == 'pk':
  163 + if self._field_is_pk(field_name):
  164 + if kwargs_pk_field_name:
  165 + raise ValueError(u'You cannot pass two values for the '
  166 + 'primary key (pk and %s)' % pk_field_name)
  167 + kwargs_pk_field_name = field_name
159 168 # always use the real field name, not always pk
160 169 field_name = pk_field_name
161 170 if field_name not in self._fields:
@@ -166,10 +175,14 @@ def __init__(self, *args, **kwargs):
166 175 raise UniquenessError(u"Field `%s` must be unique. "
167 176 "Value `%s` yet indexed." % (field.name, value))
168 177
169   - # Do instanciate
170   - for field_name, value in kwargs.iteritems():
  178 + # Do instanciate, starting by the pk and respecting fields order
  179 + if kwargs_pk_field_name:
  180 + self.pk.set(kwargs[kwargs_pk_field_name])
  181 + for field_name in self._fields:
  182 + if field_name not in kwargs or self._field_is_pk(field_name):
  183 + continue
171 184 field = getattr(self, field_name)
172   - field.proxy_set(value)
  185 + field.proxy_set(kwargs[field_name])
173 186
174 187 # --- Instanciate from DB
175 188 if len(args) == 1:
4 tests/model.py
@@ -552,6 +552,10 @@ def test_can_access_pk_with_two_names(self):
552 552 self.assertEqual(set(self.RedefinedNotAutoPkField.collection(pk=1)), set(['1', ]))
553 553 self.assertEqual(set(self.RedefinedNotAutoPkField.collection(id=2)), set(['2', ]))
554 554
  555 + def test_cannot_set_pk_with_two_names(self):
  556 + with self.assertRaises(ValueError):
  557 + self.RedefinedNotAutoPkField(name="foo", pk=1, id=2)
  558 +
555 559
556 560 class DeleteTest(LimpydBaseTest):
557 561

0 comments on commit c643d77

Please sign in to comment.
Something went wrong with that request. Please try again.