1👍
Use the models Meta
class unique_together
. You can find the excellent documentation here.
class newlist(models.Model):
class Meta:
unique_together=('user','list_name')
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slug = AutoSlugField(populate_from='list_name', default = '')
def __str__(self):
return self.list_name
def save(self,*args,**kwargs):
try:
return super(newlist,self).save(*args,**kwargs)
except IntegrityError: # This is raised if the object can't be saved.
# Your error handling code
from django.core.exceptions import PermissionDenied
raise PermissionDenied()
Source:stackexchange.com