Django的嵌套内联不工作?(Django nested inlines not working?

2019-10-23 20:25发布

我已经安装Django的巢式直列但我有问题。 这里是我的代码:

from django.contrib import admin
from nested_inline.admin import NestedStackedInline, NestedModelAdmin
from .models import Positive, Negative, Option, Question

class PositiveInline(NestedStackedInline):
    model = Positive

class NegativeInline(NestedStackedInline):
    model = Negative

class OptionInline(NestedStackedInline):
    model = Option
    inlines = [PositiveInline, NegativeInline,]

class QuestionAdmin(NestedModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_title']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [OptionInline,]

admin.site.register(Question, QuestionAdmin)

当我尝试添加一个新的问题,我有这样的错误:

AttributeError at /admin/question/question/add/

'OptionInline' object has no attribute 'queryset'

难道我做错了什么?

Answer 1:

发布本作评论,但因为它的工作,我会在这里发布此以供将来参考。 看起来这是一个错误的结果,引用和修复已应用于此: https://github.com/s-block/django-nested-inline/issues/31



Answer 2:

如果有人被卡住等待这个来通过到点子。 在您的管理,用自己的专业化如MyNestedStackedInline从而取代你NestedStackedInline的使用:

from django.contrib import admin
from nested_inline.admin import NestedStackedInline, NestedModelAdmin

import survey.models as sm


class MyNestedStackedInline(NestedStackedInline):

    def queryset(self, request):
        return self.get_queryset(request)


class AnswerInline(MyNestedStackedInline):
    model = sm.Answer
    fk_name = 'question'
    extra = 1


class QuestionInline(MyNestedStackedInline):
    model = sm.Question
    fk_name = 'questionset'
    inlines = [AnswerInline]
    extra = 1


class QuestionSetInline(MyNestedStackedInline):
    model = sm.QuestionSet
    fk_name = 'questionnaire'
    inlines = [QuestionInline]
    extra = 1


class QuestionnaireAdmin(NestedModelAdmin):
    list_display = ['id', 'description']
    inlines = [QuestionSetInline]

admin.site.register(sm.Questionnaire, QuestionnaireAdmin)


文章来源: Django nested inlines not working?