Python Django-';额外字段';在CustomModelForm中,是给予';无法查找uup';尝试加载表单时,模型内联接口出错

Python Django-';额外字段';在CustomModelForm中,是给予';无法查找uup';尝试加载表单时,模型内联接口出错,python,django,Python,Django,我有一个名为Question的应用程序,我在models.py中定义了两个模型Question和Alternative,如下所示: class Question(models.Model): question = models.CharField(max_length=255, blank=False, null=False) chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE) rating =

我有一个名为
Question
的应用程序,我在models.py中定义了两个模型
Question
Alternative
,如下所示:

class Question(models.Model):

    question = models.CharField(max_length=255, blank=False, null=False)
    chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE)
    rating = models.IntegerField(default=1)


class Alternative(models.Model):

    alternative = models.CharField(max_length=255, blank=False, null=False)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
我制作了一个自定义表单
AlternativeForm
,在这里我创建了一个额外的字段,我想在
alternativeforms
以及
Question
管理视图中显示,在该视图中,可选字段将显示在内联视图中,但额外字段值不会保存在DB中(因为我想对这些字段的值进行一些手动操作)。我的
forms.py
如下所示:

class AlternativeForm(forms.ModelForm):

    extra_field = forms.BooleanField(required=False)

    def save(self, commit=True):
        extra_field = self.cleaned_data.get('extra_field', None)
        # will do something with extra_field here...
        return super(AlternativeForm, self).save(commit=commit)

    class Meta:
        model = Alternative
        fields = '__all__'
class AlternativeInline(admin.TabularInline):
    form = AlternativeForm
    model = Alternative

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    inlines = [AlternativeInline,]

@admin.register(Alternative)
class AlternativeAdmin(admin.ModelAdmin):
    model = Alternative
    form  = AlternativeForm
在我的
admin.py
中,我在它们之间建立了一个内联关系,如下所示:

class AlternativeForm(forms.ModelForm):

    extra_field = forms.BooleanField(required=False)

    def save(self, commit=True):
        extra_field = self.cleaned_data.get('extra_field', None)
        # will do something with extra_field here...
        return super(AlternativeForm, self).save(commit=commit)

    class Meta:
        model = Alternative
        fields = '__all__'
class AlternativeInline(admin.TabularInline):
    form = AlternativeForm
    model = Alternative

@admin.register(Question)
class QuestionAdmin(admin.ModelAdmin):
    inlines = [AlternativeInline,]

@admin.register(Alternative)
class AlternativeAdmin(admin.ModelAdmin):
    model = Alternative
    form  = AlternativeForm
我得到了
属性错误:在这种情况下,无法在Alternative或Alternative Inline
上查找“extra_field”。我想
问题
应用程序管理视图的Inline视图中显示这些额外字段。有什么方法可以做到这一点,或者我当前的方法有什么错误。

谢谢。

要在管理中添加额外字段:

class YourModelAdmin(admin.ModelAdmin):

    form = YourModelForm

    fieldsets = (
        (None, {
            'fields': ('other fields here', 'extra_field',),
        }),
    )

我在推测这一点时找到了解决方案。应该在自定义字段中定义
标签
字段,如下所示,以避免此类错误
属性错误:无法在Alternative或Alternative Inline上查找“extra_field”

class AlternativeForm(forms.ModelForm):

    extra_field = forms.BooleanField(label='is_answer', required=False)

    def save(self, commit=True):
        # extra_field = self.cleaned_data.get('extra_field', None)
        # ...do something with extra_field here...
        return super(AlternativeForm, self).save(commit=commit)

实际上,这里的问题是,
额外的字段
出现在相应的模型中(可选)管理员视图,但未出现在
问题
模型管理员视图中,该视图与
备选方案
模型具有内联关系。@ManzurulOquerumi该问题是一个bug,已修复。我已经花了很多时间在这个场景上了哈哈,非常感谢先生!在我的情况下,我需要一个
隐藏字段
,它工作起来很有魅力!