doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

NotImplementedError: subclasses of BaseDatabaseOperations may require a date_trunc_sql() method #343

Open saurabh-m523 opened 4 years ago

saurabh-m523 commented 4 years ago

Getting this error when trying to use the date_hierarchy option in ModelAdmin on a DateTimeField.

Python script

# model.py
created_on = models.DateTimeField(
        blank=False,
        auto_now_add=True,
        help_text='Date and time on which this question was created.'
    )

# admin.py
date_hierarchy = 'created_on'

# project settings.py
TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Traceback

File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
    response = response.render()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\response.py", line 106, in render
    self.content = self.rendered_content
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 937, in render
    bit = node.render_annotated(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\base.py", line 904, in render_annotated
    return self.render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\contrib\admin\templatetags\base.py", line 33, in render
    return super().render(context)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\template\library.py", line 214, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\contrib\admin\templatetags\admin_list.py", line 401, in date_hierarchy
    } for day in days]
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\query.py", line 274, in __iter__
    self._fetch_all()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\query.py", line 182, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1052, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1087, in execute_sql
    sql, params = self.as_sql()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 474, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 54, in pre_sql_setup
    self.setup_query()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 45, in setup_query
    self.select, self.klass_info, self.annotation_col_map = self.get_select()
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 254, in get_select
    sql, params = self.compile(col, select_format=True)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\sql\compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\models\functions\datetime.py", line 183, in as_sql
    sql = connection.ops.date_trunc_sql(self.kind, inner_sql)
  File "E:\Saurabh\bot_dev\Bot_Dashboard\venv\lib\site-packages\django\db\backends\base\operations.py", line 110, in date_trunc_sql
    raise NotImplementedError('subclasses of BaseDatabaseOperations may require a date_trunc_sql() method.')
NotImplementedError: subclasses of BaseDatabaseOperations may require a date_trunc_sql() method.
Devansh3790 commented 4 years ago

Gettings same error but in different use case.

I have integrated celery, celery-beat in my django project with djongo engine. When opening http://localhost:8000/admin/django_celery_beat/periodictask/ getting the same error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/usr/local/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/usr/local/lib/python3.7/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/usr/local/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/templatetags/base.py", line 33, in render
    return super().render(context)
  File "/usr/local/lib/python3.7/site-packages/django/template/library.py", line 214, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/templatetags/admin_list.py", line 424, in date_hierarchy
    } for year in years]
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 182, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1052, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1087, in execute_sql
    sql, params = self.as_sql()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 474, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 54, in pre_sql_setup
    self.setup_query()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 45, in setup_query
    self.select, self.klass_info, self.annotation_col_map = self.get_select()
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 254, in get_select
    sql, params = self.compile(col, select_format=True)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 405, in compile
    sql, params = node.as_sql(self, self.connection)
  File "/usr/local/lib/python3.7/site-packages/django/db/models/functions/datetime.py", line 183, in as_sql
    sql = connection.ops.date_trunc_sql(self.kind, inner_sql)
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/operations.py", line 110, in date_trunc_sql
    raise NotImplementedError('subclasses of BaseDatabaseOperations may require a date_trunc_sql() method.')
NotImplementedError: subclasses of BaseDatabaseOperations may require a date_trunc_sql() method.
Devansh3790 commented 4 years ago

@saurabh-m523 did you find any solution or workaround on the same issue?

Devansh3790 commented 4 years ago

the issue has been resolved by adding implementation for non implemented method: djongo/operations.py:


class DatabaseOperations(BaseDatabaseOperations):
    def date_trunc_sql(self, lookup_type, field_name):
        # https://github.com/nesdis/djongo/issues/343
        return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)  

Tried to push my changes but not allowed.

@nesdis Please let me know how to create a pr for this bug fix or make this change in the package for resolve above mentioned bug.

saurabh-m523 commented 4 years ago

@Devansh3790 No, I ended up writing a SimpleListFilter for date wise filtering.

opemipoVRB commented 4 years ago

the issue has been resolved by adding implementation for non implemented method: djongo/operations.py:


class DatabaseOperations(BaseDatabaseOperations):
    def date_trunc_sql(self, lookup_type, field_name):
        # https://github.com/nesdis/djongo/issues/343
        return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)  

Tried to push my changes but not allowed.

@nesdis Please let me know how to create a pr for this bug fix or make this change in the package for resolve above mentioned bug.

@Devansh3790 has anything been done regarding this?

skpaik commented 4 years ago

Facing the same issue

subclasses of BaseDatabaseOperations may require a datetime_extract_sql() method

junshoong commented 3 years ago

here is same issue

queryset = MyModel.objects.filter(start_date__year=mydate.year, start_date__month=mydate.month)
str(queryset.query)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "...\lib\site-packages\django\db\models\sql\query.py", line 263, in __str__
    sql, params = self.sql_with_params()
  File "...\lib\site-packages\django\db\models\sql\query.py", line 271, in sql_with_params
    return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
  File "...\lib\site-packages\django\db\models\sql\compiler.py", line 505, in as_sql
    where, w_params = self.compile(self.where) if self.where is not None else ("", [])
  File "...\lib\site-packages\django\db\models\sql\compiler.py", line 422, in compile
    sql, params = node.as_sql(self, self.connection)
  File "...\lib\site-packages\django\db\models\sql\where.py", line 81, in as_sql
    sql, params = compiler.compile(child)
  File "...\lib\site-packages\django\db\models\sql\compiler.py", line 422, in compile
    sql, params = node.as_sql(self, self.connection)
  File "...\lib\site-packages\django\db\models\lookups.py", line 176, in as_sql
    lhs_sql, params = self.process_lhs(compiler, connection)
  File "...\lib\site-packages\django\db\models\lookups.py", line 167, in process_lhs
    lhs_sql, params = super().process_lhs(compiler, connection, lhs)
  File "...\lib\site-packages\django\db\models\lookups.py", line 82, in process_lhs
    return compiler.compile(lhs)
  File "...\lib\site-packages\django\db\models\sql\compiler.py", line 422, in compile
    sql, params = node.as_sql(self, self.connection)
  File "...\lib\site-packages\django\db\models\functions\datetime.py", line 48, in as_sql
    sql = connection.ops.datetime_extract_sql(self.lookup_name, sql, tzname)
  File "...\lib\site-packages\django\db\backends\base\operations.py", line 136, in datetime_extract_sql
    raise NotImplementedError('subclasses of BaseDatabaseOperations may require a datetime_extract_sql() method')
NotImplementedError: subclasses of BaseDatabaseOperations may require a datetime_extract_sql() method

when i use "__range", it is fine. but just working in my case

queryset = MyModel.objects.filter(start_date__range=(mydate, enddate))
jayashankarc3301 commented 3 years ago

Same issue.

Traceback (most recent call last): File ".../lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File ".../lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File ".../lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File ".../lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File ".../lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File ".../lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch return handler(request, *args, **kwargs) File ".../lib/python3.8/site-packages/django/views/generic/list.py", line 157, in get context = self.get_context_data() File ".../lib/python3.8/site-packages/django/views/generic/list.py", line 119, in get_context_data paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size) File ".../lib/python3.8/site-packages/django/views/generic/list.py", line 69, in paginate_queryset page = paginator.page(page_number) File ".../lib/python3.8/site-packages/django/core/paginator.py", line 76, in page number = self.validate_number(number) File ".../lib/python3.8/site-packages/django/core/paginator.py", line 54, in validate_number if number > self.num_pages: File ".../lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File ".../lib/python3.8/site-packages/django/core/paginator.py", line 103, in num_pages if self.count == 0 and not self.allow_empty_first_page: File ".../lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File ".../lib/python3.8/site-packages/django/core/paginator.py", line 97, in count return c() File ".../lib/python3.8/site-packages/django/db/models/query.py", line 412, in count return self.query.get_count(using=self.db) File ".../lib/python3.8/site-packages/django/db/models/sql/query.py", line 519, in get_count number = obj.get_aggregation(using, ['__count'])['__count'] File ".../lib/python3.8/site-packages/django/db/models/sql/query.py", line 504, in get_aggregation result = compiler.execute_sql(SINGLE) File ".../lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1162, in execute_sql sql, params = self.as_sql() File ".../lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 528, in as_sql where, w_params = self.compile(self.where) if self.where is not None else ("", []) File ".../lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 445, in compile sql, params = node.as_sql(self, self.connection) File ".../lib/python3.8/site-packages/django/db/models/sql/where.py", line 81, in as_sql sql, params = compiler.compile(child) File ".../lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 445, in compile sql, params = node.as_sql(self, self.connection) File ".../lib/python3.8/site-packages/django/db/models/lookups.py", line 193, in as_sql lhs_sql, params = self.process_lhs(compiler, connection) File ".../lib/python3.8/site-packages/django/db/models/lookups.py", line 184, in process_lhs lhs_sql, params = super().process_lhs(compiler, connection, lhs) File ".../lib/python3.8/site-packages/django/db/models/lookups.py", line 87, in process_lhs return compiler.compile(lhs) File ".../lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 445, in compile sql, params = node.as_sql(self, self.connection) File ".../lib/python3.8/site-packages/django/db/models/functions/datetime.py", line 302, in as_sql sql = connection.ops.datetime_cast_date_sql(lhs, tzname) File ".../lib/python3.8/site-packages/django/db/backends/base/operations.py", line 117, in datetime_cast_date_sql raise NotImplementedError( NotImplementedError: subclasses of BaseDatabaseOperations may require a datetime_cast_date_sql() method.