I have the following:
class AccountAdmin(models.Model):
account = models.ForeignKey(Account)
is_master = models.BooleanField()
name = models.CharField(max_length=255)
email = models.EmailField()
class Meta:
unique_together = (('Account', 'is_master'), ('Account', 'username'),)
If I then create a new AccoutnAdmin with the same username as another on the same account, instead of it giving me an error to display in the template, it breaks with an IntegrityError and the page dies. I wish that in my view, I could just go:
if new_accountadmin_form.is_valid():
new_accountadmin_form.save()
How do I conquer this problem. Is there a second is_valid() type of method that checks the DB for violation of the "unique_together = (('Account', 'is_master'), ('Account', 'username'),)" part?
I would like not to have to catch an IntegrityError in my view. That's domain logic mixed with presentation logic. It violates DRY because if I display the same form on 2 pages, I'll have to repeat the same block. It also violates DRY because if I have two forms for the same thing, I have to write the same except: again.