ESSolutions / django-mssql-backend

Django backend for Microsoft SQL Server
https://pypi.org/project/django-mssql-backend/
BSD 3-Clause "New" or "Revised" License
147 stars 51 forks source link

date_trunc_sql() takes 3 positional arguments but 4 were given #118

Open brucegibbins opened 3 years ago

brucegibbins commented 3 years ago

Hi.

Platform: Windows Python v3.9.5 Django v3.2.4
django-mssql-backend v2.8.1 pyodbc v4.0.30

This is a new upgraded environment after moving from a functional Python v3.6.x and Django v.2.1.7 which was using django-pyodbc-azure v2.1.0

It seems that if I have a ModelAdmin Class that uses the date_hierarchy attribute then we will get the error at line 204 in

\Lib\site-packages\django\db\models\functions\datetime.py", line 204

Other details that maybe of relevance are that:-

The database field type is date and not datetime

The django debug dump indicates tzname is None and the connection object is sql_server.pyodbc.base.DatabaseWrapper

I hope this is enough detail. Please advise if I can add any extra info.

Thank-you in advance

ajhobden commented 1 year ago

I've just come across this issue too.

It looks like the definition for date_trunc_sql has changed to include an optional named argument for the time zone. old: def date_trunc_sql(self, lookup_type, field_name) new: def date_trunc_sql(self, lookup_type, field_name, tzname=None)

Since there already exists code to incude the time zone this could be fixed by updating the function definition and adding an if statement to use that value if given.

    def date_trunc_sql(self, lookup_type, field_name, tzname=None):
        if tzname:
            field_name = self._convert_field_to_tz(field_name, tzname)
        CONVERT_YEAR = 'CONVERT(varchar, DATEPART(year, %s))' % field_name