简体   繁体   中英

Django Rest Framework Related Field Filtering

I just started coding in DRF and I have come across an issue where I have to search from a related foreign key.

I have 2 models, BookDetails and BookCopies.

BookCopies has BookDetails referenced in it as a foreign key.

Now I want to search BookCopies using the attribute title which is in the model BookDetails.

I used django_rest_framework_filters as follows but It gives an error when I visit http://localhost:8000/book/copy/?book_details__title=abc .

class BookFilter(FilterSet):
    class Meta:
        model = BookDetail
        fields = {
            'title': ['exact', 'in', 'startswith'],
            'author': ['exact', 'in', 'startswith'],
            'editor': ['exact', 'in', 'startswith'],
            'publisher': ['exact', 'in', 'startswith'],
            'cl_num': ['exact', 'in', 'startswith']
        }


class CopyFilter(FilterSet):
    book_details = RelatedFilter(BookFilter, name="title",  queryset=BookDetail.objects.all())

    class Meta:
        model = BookCopy
        fields = {
            'ref_id': ['exact', 'in', 'startswith']
        }

View

class CopyListAPIView(ListAPIView):
    serializer_class = CopyViewSerializer
    queryset = BookCopy.active_objects.all()
    filter_backends = [DjangoFilterBackend]
    filter_class = CopyFilter

Serializer

class CopyViewSerializer(ModelSerializer):
    class Meta:
        model = BookCopy
        fields = "__all__"

Error trace

KeyError at /book/copy/
'book_details__title'
Request Method: GET
Request URL:    http://localhost:8000/book/copy/?book_details__title=abc
Django Version: 1.10.5
Exception Type: KeyError
Exception Value:    
'book_details__title'
Exception Location: /Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/site-packages/django_filters/filterset.py in qs, line 335
Python Executable:  /Applications/MAMP/htdocs/python/01-virtual-env/bin/python
Python Version: 2.7.10
Python Path:    
['/Applications/MAMP/htdocs/library-new/services',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python27.zip',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/plat-darwin',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/plat-mac',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/lib-tk',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/lib-old',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/site-packages',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/site-packages/Django-1.10.5-py2.7.egg',
 '/Applications/MAMP/htdocs/python/01-virtual-env/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-macosx-10.12-intel.egg']
Server time:    Tue, 24 Jan 2017 13:35:36 +0000

Please Help. Thank You

To get desired behaviour you need to provide name argument equal to related_name in the model. By default it is equal to field name in FilterSet. So if it is book_details you can just skip it.

class CopyFilter(FilterSet):
    book_details = RelatedFilter(BookFilter, name="book_details", queryset=BookDetail.objects.all()

By the way there is no stack in your Error Stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM