In Django, functions today, now and localdate do not deliver the current date
corvalanlara
HOBBYOP

8 months ago

I have a project in Django I used to host in another platform and it worked perfectly. I decided to change its server to Railway but there is one issue I can't solve. The function datetime.today does not deliver the current date but the date of the last build in Railway. Even localdate() does not work properly. I assume is a problem with the server.

To try to solve it I made the function that returns a date lazy using the keep_lazy decorator. But it still does not work.

I'm here to know if anyone know what to do to solve this issue.

Solved$10 Bounty

Pinned Solution

nathanmlu
PRO

8 months ago

Hmmm, based on the behavior I'm guessing you're calling datetime.today() at module import, which basically freezes it at build time. Not sure why it is only occurring with Railway, but if you just move it inside of a function body, it should fix the issue.

basically, don’t call those funcs at module scope, justpass the callable instead (e.g. for a model use default=timezone.now with no parentheses or auto_now_add=True) or call timezone.localdate() inside your view function so it runs on every request.

3 Replies

nathanmlu
PRO

8 months ago

Hmmm, based on the behavior I'm guessing you're calling datetime.today() at module import, which basically freezes it at build time. Not sure why it is only occurring with Railway, but if you just move it inside of a function body, it should fix the issue.

basically, don’t call those funcs at module scope, justpass the callable instead (e.g. for a model use default=timezone.now with no parentheses or auto_now_add=True) or call timezone.localdate() inside your view function so it runs on every request.


nathanmlu

Hmmm, based on the behavior I'm guessing you're calling datetime.today() at module import, which basically freezes it at build time. Not sure why it is only occurring with Railway, but if you just move it inside of a function body, it should fix the issue.basically, don’t call those funcs at module scope, justpass the callable instead (e.g. for a model use default=timezone.now with no parentheses or auto_now_add=True) or call timezone.localdate() inside your view function so it runs on every request.

corvalanlara
HOBBYOP

8 months ago

I just noticed I should have given more context. I do not call from the module itself but as a default parameter in a function. I guess being there is why it's frozen at build time. I'm passing the callable now. I'll check in a few more days if it's working properly and I'll let you know!


corvalanlara

I just noticed I should have given more context. I do not call from the module itself but as a default parameter in a function. I guess being there is why it's frozen at build time. I'm passing the callable now. I'll check in a few more days if it's working properly and I'll let you know!

nathanmlu
PRO

8 months ago

Did it end up working for you?


Status changed to Solved brody 8 months ago


Loading...