Django嵌套内联不起作用?

时间:2015-07-14 06:16:40

标签: python django django-queryset attributeerror

我已经安装了django-nested-inline,但是我遇到了问题。这是我的代码:

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'

我做错了吗?

2 个答案:

答案 0 :(得分:2)

如果有人被迫等待这个来点。在您的管理员中,将您使用的NestedStackedInline替换为您自己的专业化,例如: MyNestedStackedInline因此:

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)

答案 1 :(得分:1)

将此作为评论发布,但由于它已经成功,我将在此发布此信息以供将来参考。似乎这是一个错误的结果,引用并在此处应用了修复:https://github.com/s-block/django-nested-inline/issues/31