Django Admin 内联一个对象

Django Admin Inline an object

我有两个模型 QuestionQuestionChoice。 QuestionChoice 有一个指向 Question 的 ForeignKeyfield。我想在管理视图中将这些作为内联堆栈视图,但出现错误。

型号:

class Question(models.Model):
    PROFILE = 0
    EVENT_REPORT = 1
    UNIVERSITY_REPORT = 2
    USER_REPORT = 3
    TYPE_LIST = [PROFILE, EVENT_REPORT, UNIVERSITY_REPORT, USER_REPORT]
    TYPE_CHOICES = (
        (PROFILE, 'Profile'),
        (EVENT_REPORT, 'Event Report'),
        (UNIVERSITY_REPORT, 'University Report'),
        (USER_REPORT, 'User Report'),
    )
    description = models.CharField(max_length=100)
    type =models.IntegerField(choices=TYPE_CHOICES, default=PROFILE)

    def __str__(self):
        return self.description


class QuestionChoice(models.Model):
    description = models.CharField(max_length=100)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    answer = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.question.first().description + ' ' + self.description

管理员:

admin.site.register(QuestionChoice)
class QuestionChoiceInline(admin.StackedInline):
    model = QuestionChoice
    can_delete = True
    verbose_name_plural = 'Question Choices'
    fk_name = "question"


class CustomQuestionAdmin(UserAdmin):
    inlines = (QuestionChoiceInline, )

    def get_inline_instances(self, request, obj=None):
        if not obj:
            return list()
        return super(CustomQuestionAdmin, self).get_inline_instances(request, obj)

admin.site.register(Question, CustomQuestionAdmin)

但是我收到这个错误:

ERRORS:
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E033) The value of 'ordering[0]' refers to 'username', which is not an attribute of 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'username', which is not a callable, an attribute of 'CustomQuestionAdmin', or an attribute or method on 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'email', which is not a callable, an attribute of 'CustomQuestionAdmin', or an attribute or method on 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable, an attribute of 'CustomQuestionAdmin', or an attribute or method on 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E108) The value of 'list_display[3]' refers to 'last_name', which is not a callable, an attribute of 'CustomQuestionAdmin', or an attribute or method on 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E108) The value of 'list_display[4]' refers to 'is_staff', which is not a callable, an attribute of 'CustomQuestionAdmin', or an attribute or method on 'userprofile.Question'.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E116) The value of 'list_filter[0]' refers to 'is_staff', which does not refer to a Field.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'is_superuser', which does not refer to a Field.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E116) The value of 'list_filter[2]' refers to 'is_active', which does not refer to a Field.
<class 'userprofile.admin.CustomQuestionAdmin'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.

知道我做错了什么吗?

你可以简单地这样写:

class QuestionChoiceInline(admin.StackedInline):
    model = QuestionChoice


class CustomQuestionAdmin(admin.ModelAdmin):
    list_display = ['PROFILE', ------]
    inlines = [QuestionChoiceInline, ]
    class Meta:
        model = Question


admin.site.register(Question, CustomQuestionAdmin)