clokep / django-querysetsequence

Chain multiple (disparate) QuerySets in Django
https://django-querysetsequence.readthedocs.io/
ISC License
107 stars 25 forks source link

Need help! How to pass the serializers with the QuerySetSequence? #105

Closed c0d33py closed 3 months ago

c0d33py commented 1 year ago

In my Django project, I have a FeedSerializer that contains two nested serializers (ArticleSerializer and SubjectSerializer). The goal is to use QuerySetSequence in the FeedListApiView in views.py to combine data from multiple models (Article and Ticker) and pass it to the FeedSerializer.

Here's my code:

serializers.py


class FeedSerializer(serializers.Serializer):
    article = ArticleSerializer(many=True, read_only=True)
    subject = SubjectSerializer(many=True, read_only=True)

    class Meta:
        fields = '__all__'

views.py

class FeedListApiView(ModelViewSet):
    """
    Feed list API View.
    """

    serializer_class = FeedSerializer
    pagination_class = FeedPagination

    def get_queryset(self):
        query = QuerySetSequence(
            Article.objects.all(),
            Ticker.objects.all(),
        )

        return query
clokep commented 1 year ago

That looks vaguely right according to the documentation -- are you hitting an error or something?

clokep commented 3 months ago

Closing due to lack of response.