0

I have been picking up Django for a month or two and don't have a strong education in programming, so bear with me. It may be easier than I've found it.

My project is extending the Polls app from the tutorial with a variety of different question types. Each question type will have different answer fields and options. Depending on the Answer_Type set, I want to show (or not show) relevant Inlines from the models for . I've found how to overload 'add' and 'change' views in ModelAdmin.

class Question(models.Model):  
ANSWER_TYPE_CHOICES = (
        ('CH', 'Choice'),
        ('SA', 'Short Answer'),
        ('LA', 'Long Answer'),
        ('E3', 'Expert Judgement of Probabilities'),
        ('E4', 'Expert Judgment of Values'),
        ('RR', 'Risk Rating'),
        )
answer_type = models.CharField(max_length = 2, 
                               choices= ANSWER_TYPE_CHOICES,
                               default='SA')
question_text = models.CharField(max_length=200, default = "nil")
max_entries = models.IntegerField(default = 1)
man_index = models.IntegerField(default = 0)

and (2 examples - first one needs no Inline, second one does, to allow per-question definition of risk ratings) :

    class Long_Answer(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, default = 1)
    long_answer = models.CharField(max_length=2000)
    def __str__(self):
        return self.long_answer

class Estimated_risk(models.Model):
# This is for those situations where the aggregate estimate is expected to be a more reliable expert judgment than the paramaterised (prob, cons) estimate.
# Risk value (a number) would be a real-world expected value of losses, for example
# It would be appropriate for a question to have risk and confidence
risk_value = models.DecimalField(max_digits = 4, decimal_places = 3)
risk_definition = models.CharField(max_length = 250)
risk_short_name = models.CharField(max_length = 10)

The admin.py class I wrote to realise that I don't know how to solve this problem (this does not work, but hopefully communicates what I hoped would happen):

`class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
    ('Question', {'fields': ['question_text']}),
    ('Type', {'fields': ['answer_type'], 'classes': ['collapse']}),
    ('Maximum Answers per User', {'fields': ['max_entries']}),
    ('Presentation order (unique)', {'fields': ['man_index']}),
    ]

inlines =()
def set_inlines(self, request, object_id, form_url='', extra_context = None):
    if self.fieldsets['Type'] == 'CH':
        return [ChoiceInline]
    if self.fieldsets['Type'] == 'SA':
        return None
    if self.fieldsets['Type'] == 'LA':
        return None
    if self.fieldsets['Type'] == 'E3':
        return None
    if self.fieldsets['Type'] == 'E4':
        return None
    if  self.fieldsets['Type'] == 'RR':
        return [RiskInline]                        

inlines = set_inlines(self)`
Atcrank
  • 439
  • 3
  • 11
  • https://stackoverflow.com/questions/5411565/making-inlines-conditional-in-the-django-admin?rq=1 – Atcrank Jun 14 '17 at 04:05
  • Update: This seems to be very close, but I'm struggling with implementation in my own particular ahh, idiom. [great answer] (https://stackoverflow.com/questions/5411565/making-inlines-conditional-in-the-django-admin?rq=1). Back into [Django docs] (https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin) – Atcrank Jun 14 '17 at 07:29

0 Answers0