chibisov / drf-extensions

DRF-extensions is a collection of custom extensions for Django REST Framework
http://chibisov.github.io/drf-extensions/docs
MIT License
1.47k stars 208 forks source link

Default key bit RetrieveSqlQueryKeyBit fails when lookup_url_kwarg != lookup_field #107

Closed nrabe closed 3 years ago

nrabe commented 9 years ago

File "/home/vagrant/.virtualenvs/todoapp1/local/lib/python2.7/site-packages/rest_framework_extensions/key_constructor/bits.py", line 201, in get_data lookup_value = view_instance.kwargs[view_instance.lookup_field] KeyError: 'slug'

I believe view_instance.lookup_field should be replaced with: view_instance.lookup_url_kwarg.

glynjackson commented 8 years ago

I have a different error on the same call:

class RetrieveSqlQueryKeyBit(SqlQueryKeyBitBase):
    def get_data(self, params, view_instance, view_method, request, args, kwargs):
        lookup_value = view_instance.kwargs[view_instance.lookup_field]
        try:
            queryset = view_instance.filter_queryset(view_instance.get_queryset()).filter(
                **{view_instance.lookup_field: lookup_value}
            )
        except ValueError:
            return None
        else:
            return self._get_queryset_query_string(queryset)

Is now giving:

AttributeError at /v1/catalogue/category/32

type object 'Category' has no attribute 'filter'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/v1/catalogue/category/32
Django Version:     1.8.4
Exception Type:     AttributeError
Exception Value:    

type object 'Category' has no attribute 'filter'

Exception Location:     /Users/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework_extensions/key_constructor/bits.py in get_data, line 208
Python Executable:  /Users/Documents/workspace/app/django-env/bin/python