justinlevol
justinlevol

Reputation: 1111

Django rest framework filtering "NoneType is not iterable"

I am trying to build a simple REST read only API with django and specifically, django rest framework. The goal here is to allow filtering with two simple fields : name and config.

When running that code, I get a "'NoneType' object is not iterable" error. I am wondering if I should put the line :

queryset = Component.objects.all()

Since the method get_queryset overrides that after. But I don't put that line, I get a "base_name argument not specified, and could not automatically determine the name from the viewset, as it does not have a .queryset attribute." error.

Here is my Model :

class Component(models.Model):
    name = models.CharField(max_length=200)

    def configs(self):
        return Config.objects.filter(exchange=self)

    def __unicode__(self):
        return self.name

class Config(models.Model):
    name = models.CharField(max_length=200)
    exchange = models.ForeignKey(Component)

    def sections(self):
        return Section.objects.filter(config=self)

    def __unicode__(self):
        return self.name

class Section(models.Model):
    name = models.CharField(max_length=200)
    config = models.ForeignKey(Config)

    def pairs(self):
        return KeyValues.objects.filter(section=self)

    def __unicode__(self):
        return self.name

class KeyValues(models.Model):
    key = models.CharField(max_length=200)
    value = models.CharField(max_length=999)
    section = models.ForeignKey(Section)

Here is my ViewSet :

class ComponentViewSet(viewsets.ReadOnlyModelViewSet):
    model = Component
    queryset = Component.objects.all()
    serializer_class = ComponentSerializer
    renderer_classes = (JSONRenderer, )

    def get_queryset(self):
        queryset = Component.objects.all()
        name_component = self.request.query_params.get('name', None)
        name_cfg = self.request.query_params.get('name_cfg', None)

        if name_component is not None and name_cfg is not None:
            queryset = Config.objects.filter(exchange=Component.objects.filter(name=name_component), name=name_cfg)

        return queryset

Could please tell me what I am doing wrong here ?

EDIT:

Here is the full stacktrace:

TypeError at /components/

'NoneType' object is not iterable

Request Method:     GET
Request URL:    http://127.0.0.1:8000/components/?name=Coinbase
Django Version:     1.8.3
Exception Type:     TypeError
Exception Value:    

'NoneType' object is not iterable

Exception Location:     /usr/local/lib/python2.7/dist-packages/rest_framework/filters.py in filter_queryset, line 107
Python Executable:  /usr/bin/python2.7
Python Version:     2.7.9
Python Path:    

['/home/flavio/git/web_prncpss',
 '/home/flavio/Downloads/pycharm-4.5.3/helpers/pydev',
 '/usr/local/lib/python2.7/dist-packages/pusherclient-0.3.0-py2.7.egg',
 '/usr/lib/python2.7/dist-packages',
 '/home/flavio/git/web_prncpss',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

Server time:    Sun, 13 Sep 2015 14:53:57 +0000

EDIT 2 :

Full traceback :

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/configs/?name=Coinbase&name_cfg=MMConfig

Django Version: 1.8.3
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'nested_admin',
 'accounting',
 'configs',
 'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/viewsets.py" in view
  87.             return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/mixins.py" in list
  40.         queryset = self.filter_queryset(self.get_queryset())
File "/usr/local/lib/python2.7/dist-packages/rest_framework/generics.py" in filter_queryset
  151.             queryset = backend().filter_queryset(self.request, queryset, self)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/filters.py" in filter_queryset
  107.             for search_field in search_fields

Exception Type: TypeError at /configs/
Exception Value: 'NoneType' object is not iterable

Thanks a lot,

Upvotes: 1

Views: 2585

Answers (2)

thatzprem
thatzprem

Reputation: 4767

I just commented the below in settings.py for REST_FRAMEWORK

  'DEFAULT_FILTER_BACKENDS': (
        'rest_framework.filters.DjangoFilterBackend',
    ),

Upvotes: 1

justinlevol
justinlevol

Reputation: 1111

Okay so it seems to be working good now.

I think the problem was with the settings.py file. I had this before :

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    ),
   'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
    'DEFAULT_FILTER_BACKENDS': (
        'rest_framework.filters.DjangoFilterBackend',
        'rest_framework.filters.SearchFilter'),
}

I just removed the FILTER_BACKENDS section and it worked.

Upvotes: 2

Related Questions