1

I'm completely new to Django coming from PHP and therefore struggling a little with the terminology. This makes searching for an answer quite hard. Hopefully, somebody can tell me if this can be done within the excellent Django admin.

Ok, so the idea is that I have a questionaire which will be filled in by members of the design world. The questionaire class has very few fields, title, introduction, date.

The questions have their own class, there is a limited number in this case 20 and they are the same on each questionaire.

Therefore I have an answers class, the answer belongs to both a questionaire and a question.

class Questionaire(models.Model):
    title = models.CharField(max_length=255)
    intro = models.TextField(max_length=4000)
    date_entered = models.DateField()
    date_modified = models.DateField()
    def __unicode__(self):
        return self.title

class Question(models.Model):
    title = models.CharField(max_length=255)
    number = models.IntegerField()
    def __unicode__(self):
        return self.title

class Answer(models.Model):
    response = models.TextField(max_length=4000) 
    questionaire = models.ForeignKey('articles.Questionaire')
    question = models.ForeignKey('articles.Question')
    def __unicode__(self):
        return self.response

Sorry about the formatting above What I am needing in the Questionaire admin is for each question, an answer field is available inline. If possible with the question name as the form field name.

Is this possible within Django admin?

Would be most grateful for any assistance

aprasanth
  • 1,079
  • 7
  • 20
user1016190
  • 113
  • 1
  • 1
  • 4
  • The answer to http://stackoverflow.com/questions/702637/django-admin-inline-inlines-or-three-model-editing-at-once may be of use to you. – Tom Mar 22 '12 at 21:29

1 Answers1

2

See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

class AnswerInlineAdmin(admin.StackedInline):
    model = Answer
    extra = 1

class QuestionAdmin(admin.ModelAdmin):
    ...
    inlines = [AnswerInlineAdmin]
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for attempt Chris but this doesnt really work for me. I have 20 set questions. I need an answer field for each question to appear in the questionnaire, ideally with the question title appearing as the formfield for each answer. Your suggestion adds a single answer field and then I have a dropdown to choose the question. – user1016190 Mar 23 '12 at 10:05
  • 1
    The admin doesn't support inlines within inlines at this time. You can do inline answers on the question change form, and inlines questions on the questionaire change form. But not both simultaneously. – Chris Pratt Mar 23 '12 at 14:27