Django nested inlines not working?

耗尽温柔 提交于 2020-01-16 04:48:23

问题


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?


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/31399011/django-nested-inlines-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!