I have a Django model where I want to auto-populate an md5 of another field on a custom save. It works fine until there's a duplicate value entered in the Admin, I'm getting a 50x error thrown rather than a Duplicate Error message. I expected the unique=True on the md5 field to check and catch this. Instead, I get the database unique constraint error. Feels like I'm missing something obvious but am perplexed. Is there someplace higher than save I should be checking this? Tried the pre_save signal, but had the same result.
class MailList(models.Model):
email = models.CharField(max_length=400, null=False, blank=False)
md5 = models.CharField(max_length=32, null=False, blank=True, unique=True)
def save(self, *args, **kwargs):
self.md5 = md5(self.email.strip().encode('UTF-8').lower()).hexdigest()
super(MailList, self).save()
class MailListAdmin(admin.ModelAdmin):
readonly_fields = ('date_entered', 'date_modified', )
list_display = ('email',
'date_entered', 'date_modified', 'md5',
'subscriber', )
save_on_top = True
search_fields = ['email', ]
admin.site.register(MailList, MailListAdmin)