Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DatetimeField use __year report 'int' object has no attribute 'utcoffset' #1580

Merged
merged 1 commit into from
Apr 27, 2024

Conversation

waketzheng
Copy link
Contributor

Fix DatetimeField use '__year' report 'int' object has no attribute 'utcoffset'. (#1575)
When config with "use_tz": True, the bug can be reproduced.

@abondar
Copy link
Member

abondar commented Apr 25, 2024

Hello!

Please rebase on actual develop branch and add corresponding test, that will show that problem is gone

@waketzheng
Copy link
Contributor Author

The bug can be reproduce by:

from datetime import datetime

from tortoise import Tortoise, fields, run_async
from tortoise.models import Model


class Event(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()
    created = fields.DatetimeField(null=True)

    class Meta:
        table = "event"

    def __str__(self):
        return self.name


async def run():
    # url = 'mysql://root:[email protected]:3306/test_year'
    url = "asyncpg://postgres:[email protected]:5432/test_year"
    await Tortoise.init(db_url=url, modules={"models": ["__main__"]}, use_tz=True)
    await Tortoise.generate_schemas()

    event = await Event.create(name="Test", created=datetime.now())
    print(event.created)

    events = await Event.filter(created__year=2024)
    print(events)


if __name__ == "__main__":
    run_async(run())

@coveralls
Copy link

coveralls commented Apr 26, 2024

Pull Request Test Coverage Report for Build 8856620005

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.03%) to 88.469%

Totals Coverage Status
Change from base Build 8850757408: 0.03%
Covered Lines: 5735
Relevant Lines: 6388

💛 - Coveralls

@abondar
Copy link
Member

abondar commented Apr 26, 2024

Please add such testcase

async def test_filter_by_year(self):
        with patch.dict(os.environ, {"USE_TZ": "True"}):
            obj = await testmodels.DatetimeFields.create(datetime=datetime(year=2024, month=1, day=1))
            filtered_obj = await testmodels.DatetimeFields.filter(datetime__year=2024).first()
            assert obj == filtered_obj

to tests.fields.test_time.TestDatetimeFields

@abondar
Copy link
Member

abondar commented Apr 26, 2024

Seems like mssql doesn't support extract syntax, and uses some kind of CONVERT() syntax instead
I think fixing it is out of scope for this PR, so you could just exclude mssql from this testcase

@waketzheng
Copy link
Contributor Author

@abondar done.

@abondar abondar merged commit 677fe21 into tortoise:develop Apr 27, 2024
7 checks passed
@waketzheng waketzheng deleted the fix-1575 branch June 11, 2024 03:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants