简体   繁体   中英

Django: can't iterate over and access a simple queryset

For my API, I have to find out to what projects a user has access to, and return them. I try to do this like so:

def get_queryset(self):
    user = self.request.user
    allow_all = user.is_superuser or user.is_staff or settings.API_UNSAFE
    if self.action == 'list' and not allow_all:
        projects = Project.objects.all()
        user_projects = Project.objects.none()
        for project in projects:
            permission = Permission.objects.filter(user=user.user, table_name='Project', fk=project.id)
            if permission.count() > 0:
                user_projects = user_projects | project
        return user_projects
    return Project.objects.all()

Which results in:

'Project' object is not iterable

So I used values() instead of all() . But when you use .values it's no longer possible to concat the instance with a queryset. Meaning I have to query the same project twice, resulting in this hacky approach:

projects = Project.objects.values()
user_projects = Project.objects.none()
for project in projects:
    permission = Permission.objects.filter(user=user.user, table_name='Project', fk=project['id'])
    if permission.count() > 0:
        # Wanted to use .get() here, but that threw the same error
        user_project = Project.objects.filter(id=project['id']) 
        user_projects |= user_project
return user_projects

Surely there is a better way, what am I doing wrong?

Stacktrace:

Environment:


Request Method: GET
Request URL: http://localhost:8000/api/projects/

Django Version: 2.0.6
Python Version: 3.7.0
Installed Applications:
['apps.api',
 'rest_framework',
 'apps.dashboard',
 'apps.login',
 'apps.base',
 'sass_processor',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_seed',
 'rest_framework.authtoken']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 '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 "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/viewsets.py" in view
  95.             return self.dispatch(request, *args, **kwargs)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  494.             response = self.handle_exception(exc)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/views.py" in handle_exception
  454.             self.raise_uncaught_exception(exc)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  491.             response = handler(request, *args, **kwargs)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/mixins.py" in list
  48.         return Response(serializer.data)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/serializers.py" in data
  742.         ret = super(ListSerializer, self).data

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/serializers.py" in data
  262.                 self._data = self.to_representation(self.instance)

File "/Users/Ruben/Documents/Projects/BIM_Github/bim-data-manager/venv/lib/python3.7/site-packages/rest_framework/serializers.py" in to_representation
  660.             self.child.to_representation(item) for item in iterable

Exception Type: TypeError at /api/projects/
Exception Value: 'Project' object is not iterable

The error has nothing at all to do with accessing the project ID.

The result of | between an empty queryset and an instance is the instance. So this means that when your permission exists, your user_projects variable becomes a single instance; then, later, the serializer fails as it is expecting a queryset.

You've already got a better way of doing this in a single query, but for reference if you did want to build up a list like this the better way would be to accumulate the IDs and then get them all in one query at the end; or, simply append the instances to a list.

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