You have to make additional queries since you are using concrete
inheritance from your "base" model class.

It might be a little heavy weight for just that one need, but Django
polymorphic handles this transparently for you. It is usually my go-to when
polymorphic models simplify my work.

If you're not getting any more complicated perhaps you can define the
relationship field manually and include a nested copy of your child model
in the serialized data.


On Wed, Sep 18, 2019, 5:20 PM Courtney Bracefield <
courtneybracefiel...@gmail.com> wrote:

> Hi there,
>
> I have some classes in my model.py file shown below. I would like my API
> to return a list of questions showing the slug, title and question_type of
> each question. Fetching the slug and title is trivial, but I cannot for the
> life of me access the question_type of a question because it isn't under
> the Question class, it is a field under each subclass (shown below).
>
> Anyone have any ideas? Thanks!
>
> class Question(TranslatableModel):
> """Base class for a question.
>
> Aims to be an abstract class as questions should be
> a particular subtype, though the class is not made
> completely abstract to allow retrieving all child
> objects through the InheritanceManager.
> """
>
> slug = models.SlugField(unique=True)
> title = models.CharField(max_length=SMALL)
> objects = InheritanceManager()
>
>
> class QuestionTypeProgram(Question):
> """A program based question."""
>
> QUESTION_TYPE = 'program'
>
>
> class QuestionTypeFunction(Question):
> """A function based question."""
>
> QUESTION_TYPE = 'function'
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-rest-framework+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/6da5f79c-5a51-4fe5-aa34-64a96b3571b6%40googlegroups.com
> <https://groups.google.com/d/msgid/django-rest-framework/6da5f79c-5a51-4fe5-aa34-64a96b3571b6%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CAJc1FhYnka7T_5tsq6vJfKNozHpemKmEmVrzpJ3Vo62M5NLi0w%40mail.gmail.com.

Reply via email to