Opened 4 years ago

Closed 4 years ago

#30914 closed Bug (invalid)

Queryset errors on any operation.

Reported by: jmarshall9120 Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by jmarshall9120)

I have a query set in the following view that throws an error.

The line x = workbooks_in_client.count() throws error:

ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.

Trying to slice like x = workbooks_in_client[0] also throws the error

here's the view

def home(request, client_name=None, workbook_name=None):

    user = request.user
    clients_in_user = Client.objects.filter(users=user)
    
    if clients_in_user.count():
        client = clients_in_user.filter(clientName=client_name) \
            if client_name else clients_in_user[0]

        workbooks_in_client = Workbook.objects.filter(client=client)
        x = workbooks_in_client.count()
        if x:
            workbook = workbooks_in_client.filter(workbookName=workbook_name) \
                if workbook_name else workbooks_in_client[0]
    
    context = {
        'user': user,
        'clientList': clients_in_user,
        'workbookList': workbooks_in_client,
        'currentClient': client,
        'currentWorkbook': workbook,
    }
    
    return render(request, "ClientDashboard/home.html", context)

here's the models

class Client(models.Model):

    users = models.ManyToManyField('User', blank=True)
    clientCode = models.CharField(max_length=100)
    clientName = models.CharField(max_length=200)

    def __str__(self):
        return self.clientName
    

class Workbook(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE)
    workbookName = models.CharField(max_length=100)
    workbookLink = models.CharField(max_length=1000)

    def __str__(self):
        return self.workbookName

heres the debug output

Environment:


Request Method: GET
Request URL: //localhost:3000/myClient/Performance%20Dashboard/

Django Version: 2.2.6
Python Version: 3.7.0
Installed Applications:
['ClientDashboard.apps.ClientdashboardConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'allauth.socialaccount.providers.google',
 'allauth.socialaccount.providers.facebook',
 'allauth.socialaccount.providers.microsoft',
 'widget_tweaks']
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',
 'global_login_required.GlobalLoginRequiredMiddleware']



Traceback:

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\ClientDashboard\views.py" in home
  21.         print(workbooks_in_client)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\query.py" in __repr__
  250.         data = list(self[:REPR_OUTPUT_SIZE + 1])

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\query.py" in __iter__
  274.         self._fetch_all()

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\query.py" in _fetch_all
  1242.             self._result_cache = list(self._iterable_class(self))

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\query.py" in __iter__
  55.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1087.             sql, params = self.as_sql()

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  489.                 where, w_params = self.compile(self.where) if self.where is not None else ("", [])

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\sql\compiler.py" in compile
  405.             sql, params = node.as_sql(self, self.connection)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\sql\where.py" in as_sql
  81.                 sql, params = compiler.compile(child)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\sql\compiler.py" in compile
  405.             sql, params = node.as_sql(self, self.connection)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\fields\related_lookups.py" in as_sql
  130.         return super().as_sql(compiler, connection)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\lookups.py" in as_sql
  163.         rhs_sql, rhs_params = self.process_rhs(compiler, connection)

File "C:\Users\jmarshall\source\repos\myCompany\MDGOnline\env\lib\site-packages\django\db\models\lookups.py" in process_rhs
  257.                     'The QuerySet value for an exact lookup must be limited to '

Exception Type: ValueError at /myClient/Performance Dashboard/
Exception Value: The QuerySet value for an exact lookup must be limited to one result using slicing.

Django Version:

django.VERSION
(2, 2, 6, 'final', 0)

Python Version:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32

Change History (2)

comment:1 by jmarshall9120, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed
Summary: Queryset errors on any operationQueryset errors on any operation.
Type: UncategorizedBug

It looks that you have at least two clients with the same name, it's not an issue in Django. Please don't use trac as a support channel.

Closing per TicketClosingReasons/UseSupportChannels.

Note: See TracTickets for help on using tickets.
Back to Top