Skip to content

Instantly share code, notes, and snippets.

@azhtom
Created May 25, 2016 16:40
Show Gist options
  • Save azhtom/6e1d9d77cab69bff10da7331c9660a94 to your computer and use it in GitHub Desktop.
Save azhtom/6e1d9d77cab69bff10da7331c9660a94 to your computer and use it in GitHub Desktop.
Django: Limit number of model instances to be created
def validate_max_rows(obj):
max_num = 5
model = obj.__class__
if (model.objects.count() == max_num and obj.id is None):
raise ValidationError(u"oops! max num is %d" % max_num)
class Model(models.Model):
# Fields
def clean(self):
validate_max_rows(self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment