0

I have 2 question models extending the base Question model:

class Question(models.Model):
    title = models.CharField(blank=False, max_length=200)
    order = models.PositiveIntegerField(default = 0)
    is_actuve = models.BooleanField(default = False)

class OptionQuestion(Question):
    is_bulletpoint = models.BooleanField(default=False)
    has_others = models.BooleanField(default=False)
    has_context = models.BooleanField(default=False)
    options = ArrayField(models.CharField(max_length=100), verbose_name='گزینه‌ها')


class ItemQuestion(Question):
    pass

I originally had the Question class abstract but i needed a unique id across all questions, and now i am in need of a ListAPIView for both of them. I have 2 serializers for OptionQuestion and ItemQuestion respectively. I just need to implement a view which would handle a request for a list of questions and handling them with the proper serializer each.

class QuestionListView(ListAPIView):
    serializer_class_OptionQuestion = OptionQuestionSerializer
    serializer_class_ItemQuestion = ItemQuestionSerializer

    def get_queryset_OptionQuestion(self):
        #???
11
  • Sometimes generic class-based views are great, but sometimes you have to resort to building your own. It think that's the case here. The ListAPIView isn't made for two lists, so you should just build your own view that returns the correct JSON. Take a step back, use the the generic APIView instead and implement your own get() method. Commented Jan 31, 2019 at 11:32
  • Or you may have a look at django-polymorphic Commented Jan 31, 2019 at 11:45
  • @dirkgroten thanks i will do that Commented Jan 31, 2019 at 11:57
  • @gpichot i really like it and would use it, though when i said that to my supervisor yesterday, he disagreed saying it would lower performance probably. Commented Jan 31, 2019 at 11:58
  • You could use this trick to automatically fetch the related subclasses just when you need them. Or use InheritanceManager which does exactly the same, when requested, and doesn't change normal behaviour, i.e. no performance issue. You could use that then in the ListApiView's get_queryset() method. Commented Jan 31, 2019 at 12:02

1 Answer 1

1

You can try this

class QuestionListView(ListAPIView):
   serializer_class_OptionQuestion = OptionQuestionSerializer
   serializer_class_ItemQuestion = ItemQuestionSerializer

   def get(self, request, *args, **kwrgs):
      if request.method == 'GET':
          query1 = Question.objects.all()
          query2 = OptionQuestion.objects.all()

          seralizer1 = self.serializer_class_OptionQuestion(query1)
          seralizer2 = self.serializer_class_ItemQuestion(query2)

          return Response(
             {
               'data1':seralizer1.data,
               'data2':seralizer2.data,
             }
          )

Hope it work for your problem

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.