MongoEngine / django-mongoengine

django mongoengine integration
Other
750 stars 206 forks source link

'_thread._local' object has no attribute 'no_dereferencing_class' for simple querryset #204

Closed NuxitBrain closed 8 months ago

NuxitBrain commented 8 months ago

hello everyone

maybe this error on my part put in didn't success to fix it : here is my model :

#app/models.py

from django_mongoengine import Document, fields

class Project(Document):

    name = fields.StringField(max_length=100)
    goal = fields.StringField(max_length=1000)
    description = fields.StringField(max_length=1000)

when i querry on djnago shell everything is fine :

In [10]: test = Project.objects

In [11]: print(test)
[<Project: test>, <Project: test2>]

But with this view :

from app.models import *

def projects(request):
    projects = Project.objects
    print(projects)
    return render(request,"app/hello_world.html")

I get this error :

AttributeError: '_thread._local' object has no attribute 'no_dereferencing_class'

Did i miss something with mongoengine or this is a bug of django_mongoengine ?

python version : 3.11.7 django : 4.2 django mongoengine : 0.6.0b2

last-partizan commented 8 months ago

Hi, please provide full traceback so we can see where it's coming from.

last-partizan commented 8 months ago

I don't see no_dereferencing_class in our code, so that's probably something else.

NuxitBrain commented 8 months ago

Here is the full traceback

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/metadatacell/projects/

Django Version: 4.2.0
Python Version: 3.11.7
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django_mongoengine',
 'django_mongoengine.mongo_auth',
 'django_mongoengine.mongo_admin',
 'bootstrap3',
 'metadatacell')
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 (most recent call last):
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\aneveu\OneDrive - BolloreSA\Documents\Data_travail\Programme\DAnalyse\frontend\metadatacell\views.py", line 35, in projects
    print(projects)
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\mongoengine\queryset\queryset.py", line 73, in __repr__
    self._populate_cache()
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\mongoengine\queryset\queryset.py", line 128, in _populate_cache
    self._result_cache.append(next(self))
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\mongoengine\queryset\base.py", line 1636, in __next__
    _auto_dereference=self._auto_dereference,
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\mongoengine\queryset\base.py", line 1768, in _auto_dereference
    should_deref = not no_dereferencing_active_for_class(self._document)
  File "C:\Users\aneveu\AppData\Local\anaconda3\envs\django2\Lib\site-packages\mongoengine\context_managers.py", line 28, in no_dereferencing_active_for_class
    return cls in thread_locals.no_dereferencing_class

Exception Type: AttributeError at /metadatacell/projects/
Exception Value: '_thread._local' object has no attribute 'no_dereferencing_class'

and in fact i do not use no_dereferencing_class. This is a very simple test of geting a variable from a document

last-partizan commented 8 months ago

https://github.com/MongoEngine/mongoengine/issues/2799

yeah, that's related to mongoengine

Try downgrading it to 0.27.0.

bagerard commented 8 months ago

MongoEngine 0.28.1 will be published in a few minutes with a bugfix

NuxitBrain commented 8 months ago

I confirm that it fix this bug ! thank you very much for the help. Maybe by the setup.py should by updated to avoid this problem again ?

last-partizan commented 8 months ago

That's unnecessary.