question-mark

Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DynamicRelationField ignores queryset param

See original GitHub issue

Issue Description

The code below always returns all the units associated with the curriculum being serialized, and ignores the filter.

class CurriculumSerializer(DynamicModelSerializer):
    units = DynamicRelationField('UnitSerializer', many=True, queryset=Unit.objects.filter(id=1))

    class Meta:
        model = Curriculum
        name = 'curriculum'
        fields = ('id', 'units')


class UnitSerializer(DynamicModelSerializer):
    class Meta:
        model = Unit
        name = 'unit'
        fields = ('id',)

When offloading the queryset to a function with an embedded print or ipdb, such as

def get_unit_queryset(field):
    print 'IS THIS EVER HAPPENING?"
    return Unit.objects.filter(id=1)

the result is the same and the print statement is never executed. The function is never called.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:4
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
TwunTeecommented, Apr 18, 2019

Most of the magic happens when using filter_queryset where it uses the DynamicFilterBackend. When you are using list_route or detail route I guess you wont be filtering with self.filter_queryset, queryset argument wont be called and you also lose out on the prefetch optimization etc. For anyone looking out for solution make a subclass of the DynamicFilterBackend and call filter_queryset which would set the serializer too else it would end up getting it from get_queryset method. I found it cleaner to use multiple model viewset for my use case and subclassing the backend to be really a messed up hack. If i come across a need for it later on or if i find time, will make a gist of it. If any one wants to write a solution meanwhile I can help out

1reaction
TwunTeecommented, May 3, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic REST Tutorial — Dynamic REST 1.3.15 documentation
Notes: The function mapped to a queryset should accept one parameter, which is the field (i.e. a DynamicRelationField instance) and return a QuerySet...
Read more >
Why queryset.only() is ignored when using serializers?
I am using Django and REST Framework for my app. I want to retrieve distinct values of 1 column in a django model,...
Read more >
ordering direction · Issue #171 · AltSchool/dynamic-rest - GitHub
When looking up the QuerySet documentation to achieve this, I found out you can add a minus sign before the column names given...
Read more >
django-rest-models Documentation - Read the Docs
Just keep in mind that the backend is not a SGDB and it may not be performant on all queryset, and that some...
Read more >
dynamic-rest 1.3.14 - PyPI
location = DynamicRelationField('LocationSerializer') ... "sideload") relationships can be expressed with the `include[]` query parameter.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found