diff --git a/redisco/models/attributes.py b/redisco/models/attributes.py index 633131b..9496e9c 100755 --- a/redisco/models/attributes.py +++ b/redisco/models/attributes.py @@ -6,7 +6,7 @@ from datetime import datetime, date from dateutil.tz import tzutc, tzlocal from redisco.containers import List -from exceptions import FieldValidationError +from exceptions import FieldValidationError, MissingID __all__ = ['Attribute', 'CharField', 'ListField', 'DateTimeField', 'DateField', 'ReferenceField', 'IntegerField', @@ -106,9 +106,15 @@ def validate(self, instance): def validate_uniqueness(self, instance, val): encoded = self.typecast_for_storage(val) - same = len(instance.__class__.objects.filter(**{self.name: encoded})) - if same > 0: - return (self.name, 'not unique',) + matches = instance.__class__.objects.filter(**{self.name: encoded}) + if len(matches) > 0: + try: + instance_id = instance.id + no_id = False + except MissingID: + no_id = True + if (len(matches) != 1) or no_id or (matches.first().id != instance.id): + return (self.name, 'not unique',) class CharField(Attribute):