开发者

Django Models and Admin interface question

开发者 https://www.devze.com 2023-03-06 14:48 出处:网络
I am preparing a Examination website for my students. Simple website, with use of Django\'s admin interface to create the Question paper.

I am preparing a Examination website for my students. Simple website, with use of Django's admin interface to create the Question paper.

I have following models:

class Paper(models.Model):
    name = models.CharField(max_length=2000, unique=False)
    short_desc = models.TextField(unique=False)

class Question(models.Model):
    text = models.TextField(unique=False)
    order = models.IntegerField(unique=True)
    paper = models.ForeignKey(Paper, unique=False)

While adding questions to the paper, I want that I s开发者_运维问答hould be able to add the question from the Paper's admin interface itself, by clicking a "+" sign or some kind of "add more questions" etc.

In my current setup, I have to first create the paper and then go into Question interface and add them one by one (and heaven's forbid) if I lose their order number.

Remember, each Question belongs to a Paper and it is not a ManyToMany thing here.

Do I have to modify the admin in any way or am I doing it wrong?

Thanks.


The admin interface has the ability to edit models on the same page as a parent model. These are called inlines. See InlineModelAdmin

Create a QuestionInline:

class QuestionInline(admin.TabularInline):
    model = Question

and in PaperAdmin add:

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


InlineModelAdmin is what you're after. See http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects for the details

0

精彩评论

暂无评论...
验证码 换一张
取 消