Home > front end >  Django Using TabularInline in Admin Panel
Django Using TabularInline in Admin Panel

Time:02-25

I'm making a django app. It has multiple tests with multiple quesiton each. every question has an answer.

models.py

class Test(models.Model):
    name = models.CharField(max_length=200)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=None, null=True, blank=True)
    date_posted = models.DateTimeField(auto_now_add = True)

    def get_questions(self):
        return self.question_set.all()

    def __str__(self):
        return self.name

class Question(models.Model):

    text = models.CharField(max_length=200, null=True)
    test = models.ForeignKey(Test, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add = True)


    def get_answer(self):
        return self.answer_set.all()

    def __str__(self):
        return self.text

class Answer(models.Model):
    text = models.CharField(max_length=200)
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='parent')
    def __str__(self):
         return self.text

I'd like to be able to manage whole test (making/changing questions and answers) from one django admin "view".

I tried to do this like that:

admin.py

class QuestionInline(admin.TabularInline):
    model = Question


class TestAdmin(admin.ModelAdmin):
    inlines = [
        QuestionInline,
    ]

admin.site.register(Test, TestAdmin)

but it only allows me to change/add test's questions.

enter image description here

what should I change/add to be able to manage whole test - including answers managing system from one page ?

ofc I can do it by making another admin page:


class AnswerInline(admin.TabularInline):
    model = Answer


class QuestionAdmin(admin.ModelAdmin):
    inlines = [
        AnswerInline,
    ]

admin.site.register(Question, QuestionAdmin)

But i would like to do it from only one page

CodePudding user response:

Checkout https://github.com/theatlantic/django-nested-admin it's might be what you are looking for.

  • Related