Django 嵌套内联不起作用?
Django nested inlines not working?
我已经安装了 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'
我是不是做错了什么?
将此作为评论发布,但既然它成功了,我将 post 放在这里以供将来参考。似乎这是错误的结果,已在此处引用并应用了修复程序:https://github.com/s-block/django-nested-inline/issues/31
以防万一有人被困在等待这个通过 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)
我已经安装了 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'
我是不是做错了什么?
将此作为评论发布,但既然它成功了,我将 post 放在这里以供将来参考。似乎这是错误的结果,已在此处引用并应用了修复程序:https://github.com/s-block/django-nested-inline/issues/31
以防万一有人被困在等待这个通过 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)