having some validation logic in abstract class

10 views
Skip to first unread message

Emmanuel Jannetti

unread,
Nov 23, 2012, 11:56:16 AM11/23/12
to django...@googlegroups.com
Hi all, 

The question is how to add some validation statement in an upper/abstract class against all instances of subclasses.

here is the model

class UpperAbstract(models.Model):

  def verifyUnique(value):
    if value:
      len(UpperAbstract.objects.filter(myuniquefield=value)) > 0:
         raise ValidationError(...)

  myuniquefield = CharField(max_length=15, blank=True,validators=[verifyUnique]))

  class Meta:
abstract = True

class Foo(UpperAbstract):
      myFoofield = CharField(max_length=15)

class Bar(UpperAbstract):
      myBarfield = CharField(max_length=15)


Validation and store of any instance of Foo or Bar is failing as UpperAbstract do not have objects attribute.
I can imagine why  but my question is then how to achieve this type of validation ? 

PS : my first goal was to be able to define field as 'unique if not None'.

thank in advance 
Regards
manu


Reply all
Reply to author
Forward
0 new messages