I have a Django 3.0 Model and associated Admin Add Form. I would like to validate entered data against other data being entered at the same time, the way you can for normal Forms -- making sure two password fields match, for example, or making sure the email and backup email fields are different.
It seems like the best way to do this is to extend the clean()
function for the Add page's Form, because that's the function you'd use for a normal Form. The documentation seems to give hints that this might be possible, but it doesn't give enough information to put it all together. Web searches for "Django extend Admin Form" return results only for extending the template.
How do I extend a Django Admin Add Form's clean()
function?
Edit: For concreteness, here's an example of the Model and ModelAdmin the answer should apply to:
# models.py
class Questions(models.Model):
question = models.CharField(
help_text='Text of question'
)
class Choices(models.Model):
q = models.ForeignKey(
Questions,
on_delete=models.CASCADE
)
choice = models.CharField(
help_text='Text of choice'
)
# admin.py
class ChoicesInQuestions(admin.TabularInline):
model = Choices
extra = 4
max_num = 4
@admin.register(Questions)
class QuestionAdmin(admin.ModelAdmin):
inlines = [ChoicesInQuestions]
There are four Choices on the Question Add page. I'd like to simultaneously validate them against each other at the time of submission, just like I can do with any regular form using its clean()
function.