Django Models and Admin interface question
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
精彩评论