Reputation: 43
I have an admin form that allows and end user to add a quiz, and the quiz has answers. When you edit the quiz, the quiz answer section has 4 lines for quiz answers. The first 3 are numbered 0, 1, 2 and the fourth is __prefix__
.
Example:
<input name="quizanswer_set-0-text" value="one" class="vTextField" maxlength="255" type="text" id="id_quizanswer_set-0-text" />
<input id="id_quizanswer_set-__prefix__-text" type="text" class="vTextField" name="quizanswer_set-__prefix__-text" maxlength="255" />
When the form is processed, it skips the fourth input because, I'm assuming, the prefix is not what it is looking for.
Template Code:
{% for inline_admin_form in inline_admin_formset %}
{{ inline_admin_form.pk_field.field }} {{ inline_admin_form.fk_field.field }}
Form:
class QuizAnswerInlineFormSet(forms.models.BaseInlineFormSet):
def is_valid(self):
# make sure errors are populated
_ = self.errors
return super(QuizAnswerInlineFormSet, self).is_valid()
def clean(self):
super(QuizAnswerInlineFormSet, self).clean()
answer_count = 0
correct_count = 0
for form in self.forms:
if not hasattr(form, 'cleaned_data'):
continue
if not form.cleaned_data:
continue
if not form.is_valid():
continue
if form.cleaned_data[forms.formsets.DELETION_FIELD_NAME]:
continue
answer_count += 1
if form.cleaned_data.get("is_correct", False):
correct_count += 1
if answer_count < 2:
raise ValidationError("Questions must have at least 2 answers")
if correct_count != 1:
raise ValidationError("There must be exactly one correct answer")
Does anyone have an idea of why this would be printing __PREFIX__
rather than a number? Is there something I need to set for this? I didn't originally make the form, I'm just trying to fix it and I'm not overly familiar with this type of form.
Upvotes: 1
Views: 1545
Reputation: 9
In my point of view PREFIX is used to increment the index of the rows. when we add new rows.
<div class="empty-form"></div>
is cloned and the prefix is changed by jQuery in Admin Panel.
django\contrib\admin\static\admin\js\inlines.js
view this file. This is used for dynamic inline tabular.
Upvotes: 0
Reputation: 310
I had the same issue. It turns out that row shouldn't be displayed. It is used as a template by the javascript when adding new rows and the prefix is replaced.
I found that I wasn't picking up forms.css
properly so it couldn't find the definition for the empty-form
class which has display: none
.
Upvotes: 1