Django nested inlines not working?

2019-08-10 20:38发布

I've installed django-nested-inline but I have problems. Here's my code:

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)

When I try to add a new Question, I have this error:

AttributeError at /admin/question/question/add/

'OptionInline' object has no attribute 'queryset'

Am I doing something wrong?

2条回答
贼婆χ
2楼-- · 2019-08-10 21:19

In case anyone is stuck waiting for this to come through to pip. In your admin, replace your use of NestedStackedInline with your own specialization e.g. MyNestedStackedInline thus:

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)
查看更多
乱世女痞
3楼-- · 2019-08-10 21:42

Posted this as a comment, but since it worked out I'll post this here for future reference. Seems like this is the result of a bug, referenced and a fix has been applied here: https://github.com/s-block/django-nested-inline/issues/31

查看更多
登录 后发表回答