[Answered ]-Django: setting a max number of unique fields in a Model

2👍

If you’re using your own views, you can use model validation as a place to put this code. (but remember you have to call clean yourself)
http://docs.djangoproject.com/en/dev/ref/models/instances/#validating-objects

If you want this enforced in the admin, you’ll need to override the admin forms.
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin

# pseudocode. If you went the route of admin forms, 
# the ModelForms would use self.cleaned_data[attr]

if self.page == self.also_page: 
    raise models(or forms).ValidationError("Pages are the same")

elif Also_Viewed.objects.filter(page=self.page).count() >= 24:
    raise models(or forms).ValidationError("Can not have more than 24 items for %s" % self.page)

# let the unique_together catch uniqueness.

Leave a comment