Django Inline Admin Formset的额外条目具有前缀而不是数字 [英] Django Inline Admin Formset extra entry has prefix instead of number
问题描述
我有一个管理表单,允许和最终用户添加一个测验,测验有答案。编辑测验时,测验答案部分有4行测验答案。前3个编号为0,1,2,第四个为 __前缀__
。
示例:
< input name =quizanswer_set-0 -textvalue =oneclass =vTextFieldmaxlength =255type =textid =id_quizanswer_set-0-text/>
< input id =id_quizanswer_set -__ prefix __- texttype =textclass =vTextFieldname =quizanswer_set -__ prefix __- textmaxlength =255/>
当表单处理时,它跳过第四个输入,因为我假设前缀是不是它正在寻找。
模板代码:
inline_admin_form inline_admin_formset%的$%
{{inline_admin_form.pk_field.field}} {{inline_admin_form.fk_field.field}}
形式:
class QuizAnswerInlineFormSet(forms.models.BaseInlineFormSet):
def is_valid (self):
#确保错误填充
_ = self.errors
返回超级(QuizAnswerInlineFormSet,self).is_valid()
def clean(self):
super(QuizAnswerInlineFormSet,self).clean()
answer_count = 0
correct_count = 0
.forms:
如果不是hasattr(form,'clean_data'):
continue
如果不是form.cleaned_data :
继续
如果不是form.is_valid():
继续
如果form.cleaned_data [forms.formsets.DELETION_FIELD_NAME]:
继续
answer_count + = 1
如果form.cleaned_data.get(is_correct,False):
correct_count + = 1
如果answer_count< 2:
raise ValidationError(问题必须至少有2个答案)
如果correct_count!= 1:
raise ValidationError(必须有一个正确答案)有没有人知道为什么会打印 __ PREFIX __
而不是数字?有没有什么我需要为此设定?我最初没有写这个表单,我只是想解决这个问题,而且我不太熟悉这种形式。解决方案我有同样的问题。原来不应该显示该行。当添加新行时,它被用作模板,并且前缀被替换。
我发现我没有拿起表单.css
正确地找不到空格式
类的定义,该类具有 display:none
。
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.
解决方案 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
.
这篇关于Django Inline Admin Formset的额外条目具有前缀而不是数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!