1

Building a custom form application I got the following models.py

#models.py
class Question(models.Model):
    question_title = models.TextField(...)


class Answer(models.Model):
    answer_title = models.TextField(...)
    question = models.ForeignKey(Question)

    class Meta:
        abstract = True


class BoolAnswer(Answer):
    result = models.BooleanField(...)
    if_answer = models.TextField(...)
    else_answer = models.TextField(...)

class NumberAnswer(Answer):
    answer = models.DecimalField(...)

class MultipleChoiceAnswer(...):
    ....

Now I am trying to get all answers belonging to one question using:

question.answer_set.all()

which does not exist. There are only

question.boolanswer_set
question.numberanswer_set
....

etc.

Is there an elegant way to get all answers?

ProfHase85
  • 11,763
  • 7
  • 48
  • 66
  • 1
    `answer_set` does not exist unless you have a `related_name` attribute set. Can you edit the question with the traceback ? – karthikr Dec 05 '13 at 18:39
  • 1
    [This is the `InheritenceModelManager`](https://django-model-utils.readthedocs.org/en/latest/managers.html#inheritancemanager) mentioned in one of the answers in the duplicate link – Timmy O'Mahony Dec 05 '13 at 18:40

1 Answers1

0

As mentioned by @TimmyO'Mahony : One needs

https://django-model-utils.readthedocs.org/en/latest/managers.html#inheritancemanager

the following code works: (thx @kathikr):

class Answer(models.Model):
    answer_title = models.TextField(...)
    question = models.ForeignKey(Question, related_name='answer_set')

    class Meta:
        abstract = True

    objects = InheritanceManager()

Now question.answer_set.all() gets all answer subclass objects

ProfHase85
  • 11,763
  • 7
  • 48
  • 66