gpt4 book ai didi

python - Django 嵌套内联不起作用?

转载 作者:行者123 更新时间:2023-11-30 23:09:05 24 4
gpt4 key购买 nike

我已经安装了 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'

我做错了什么吗?

最佳答案

万一有人被困在等待这件事通过 pip。在您的管理员中,将您对 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)

关于python - Django 嵌套内联不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31399011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com