I have a Django model that looks like this.
class Solution(models.Model):
'''
Represents a solution to a specific problem.
'''
name = models.CharField(max_length=50)
problem = models.ForeignKey(Problem)
description = models.TextField(blank=True)
date = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ("name", "problem")
I use a form for adding models that looks like this:
class SolutionForm(forms.ModelForm):
class Meta:
model = Solution
exclude = ['problem']
My problem is that the SolutionForm
does not validate Solution
's unique_together
constraint and thus, it returns an IntegrityError
when trying to save the form. I know that I could use validate_unique
to manually check for this but I was wondering if there's any way to catch this in the form validation and return a form error automatically.
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…