2

Here is a django model:

class Question(models.Model):
    text = models.TextField(verbose_name=u'Вопрос')
    is_free_text = models.BooleanField(verbose_name=u'Ответ в виде текста?')
    options = models.ManyToManyField('QuestionOption', related_name='option',null=True, blank=True)

and options model it uses:

class QuestionOption(models.Model):
    text = models.TextField(verbose_name=u'Вариант ответа')
    score = models.IntegerField()

Django admin, when you attempt to add a new Question will show you a form where you can enter question text, tick the is_free_text checkbox and a listbox with existing options and a plus icons to let you add new ones.

Is there a way to disable this behaviour and force users to always add new options instead of being able to as well select the existing ones? Ideally, I don't want them to see existing options as this is sometimes confusing. Like a line of text and a text box for score and a plus icon to add a new record of option text and its score?

I'm trying to utilize existing django admin as much as possible and I think I saw this done exactly the way I'm trying to express here but can't remember where.

defuz
  • 26,721
  • 10
  • 38
  • 60
abolotnov
  • 4,282
  • 9
  • 56
  • 88

1 Answers1

0

As I understand your question, you should use InlineModelAdmin:

class QuestionOptionInline(admin.StackedInline):
    model = QuestionOption

    extra = 1 # show only one QuestionOption form

    def queryset(self, request):
         # hack: don't show existed QuestionOption
         return QuestionOption.objects.none()

class QuestionAdmin(admin.ModelAdmin):
    fields = ['text', 'is_free_text']
    inlines = [QuestionOptionInline]
defuz
  • 26,721
  • 10
  • 38
  • 60
  • Thanks for the hint, it's not quite working though - getting ' has no ForeignKey to ' – abolotnov Oct 09 '12 at 19:59
  • I fixed this by removing 'options' from Question class and adding a FK to Question in QuestionOption, what worries me now is that when you go edit a Question its options don't show up - is that normal? – abolotnov Oct 09 '12 at 20:19
  • as conclusion for all this - it won't quite work with many-to-many relationship on the models (but can be cured by a bit of a hack) and when related models as related to the main one via a FK, the queryset override is not really needed. – abolotnov Oct 09 '12 at 20:22