django admin - force users to create new records rather then utilizing existing ones

392 views Asked by At

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.

1

There are 1 answers

3
defuz On BEST ANSWER

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]