python - django model filter with validate_unique -
there error says 'invalid syntax' newbie django , python... error in code? first if-block works second doesn't...
class membership(models.model): person = models.foreignkey(person) group = models.foreignkey(group) is_joined = models.booleanfield(default = false) #if true, joined, else wish_member is_master = models.booleanfield(default = false) def __str__(self): if(self.is_joined): return self.person.name + " member in group " + self.group.name return self.person.name + " wishes join in group " + self.group.name def validate_unique(self, *args, **kwargs): #super(person, self.person).validate_unique(*args, **kwargs) #tests if there same person in same group. if(self.__class__.objects.filter(person_id = self.person.id, group_id = self.group.id,).exists()): raise validationerror( { non_field_errors: ('the person exists.',) } ) elif(self.__class__objects.filter(is_master = true, group_id = self.group.id,).exists()): raise validationerror( { non_field_errors: ('the master exists.') })
edits: noticed dot missing.leaving there credit found it. error occurs in:
elif(self.__class__.objects.filter(is_master = true, group_id = self.group.id,).exists()):
you can use unique_together
make set of fields, taken together, unique. in case, specify
class membership(models.model): .... class meta: unique_together = ( ("person", "group"), ("is_master", "group"), )
this make sure person
, group
, is_master
, group
unique together. read more information here.
Comments
Post a Comment