Skip to content

Commit

Permalink
Correct issue iamteem#20 from original fork iamteem#20 (thanks to @ja…
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed Oct 16, 2011
1 parent 2cf37d6 commit 9a4bab6
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions redisco/models/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
from datetime import datetime, date
from redisco.containers import List
from exceptions import FieldValidationError
from exceptions import FieldValidationError, MissingID

__all__ = ['Attribute', 'CharField', 'ListField', 'DateTimeField',
'DateField', 'ReferenceField', 'IntegerField',
Expand Down Expand Up @@ -110,9 +110,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):
Expand All @@ -127,12 +133,12 @@ def validate(self, instance):
super(CharField, self).validate(instance)
except FieldValidationError as err:
errors.extend(err.errors)

val = getattr(instance, self.name)

if val and len(val) > self.max_length:
errors.append((self.name, 'exceeds max length'))

if errors:
raise FieldValidationError(errors)

Expand Down Expand Up @@ -375,7 +381,7 @@ def attname(self):

@property
def related_name(self):
return self._related_name
return self._related_name

def validate(self, instance):
val = getattr(instance, self.name)
Expand Down

0 comments on commit 9a4bab6

Please sign in to comment.