Skip to content

Instantly share code, notes, and snippets.

@ekinertac
Last active December 16, 2015 23:29
Show Gist options
  • Save ekinertac/5514627 to your computer and use it in GitHub Desktop.
Save ekinertac/5514627 to your computer and use it in GitHub Desktop.
Django: Validate Model Instance Count
def validate_count(obj):
model = obj.__class__
if model.objects.count() >= 8:
raise ValidationError('You can add only 8 slider objects %s' % model.__name__)
class Slider(models.Model):
# Model fields
def clean(self):
validate_count(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment