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

Displays Gregorian date #249

Open
omidshojaee opened this issue Jun 8, 2024 · 6 comments
Open

Displays Gregorian date #249

omidshojaee opened this issue Jun 8, 2024 · 6 comments

Comments

@omidshojaee
Copy link

Configurations

  • Python version: 3.12.3
  • Django version: 5.0.6
  • django_jalali version: 6.2.1
  • OS: Windows 11
  • Database: sqlite

Here's my setup:

models.py:

from django.utils import timezone
from django_jalali.db import models as jmodels

class Post(models.Model):
    publish = jmodels.jDateTimeField(default=timezone.now)

With the above setup, the add form in the admin site still displays the Gregorian date:

image

And the datepicker is showing the wrong date (it shows Shahrivar because now it is month 6 of Gregorian calendar):

image

Ironically, in the same project, in my custom user model I have this:

models.py

date_joined = jmodels.jDateTimeField(default=timezone.now)

And this is how it looks in the Admin site:

image

So it works without any issue.

The only difference between those two models is that one of them is my own, and the other one is a subclass of Django's User model.

@omidshojaee
Copy link
Author

I figured out what is the issue.

If I define the field like this:

publish = jmodels.jDateTimeField()

Then this is what I have in the admin site (i.e the datepicker works correctly)

image

However if I change the field to this:

publish = jmodels.jDateTimeField(default=timezone.now)

Then the datepicker does not work:

image

So, is that intentional? What is the solution here?

@hramezani
Copy link
Collaborator

have you tested jmodels.jDateTimeField(auto_now_add=True)?

@omidshojaee
Copy link
Author

Yes it works however the field disappears from the admin site because auto_now_add means set the field value only once during object creation and it is not meant to be edited later.

I need to set the field's value automatically, but edit it later if I need to. That's why timezone.now is the suitable option.

@slashmili
Copy link
Owner

What if you use

    publish = jmodels.jDateTimeField(default=jdatetime.datetime.now)

if you want to get the timezone based on the setting,s you need to write a function like

def current_datetime():
  dt = jdatetime.datetime.now()
  new_dt = .. do something with dt to get the timezone from settings and apply it on the datetime
  new_dt


class Post(models.Model):
    publish = jmodels.jDateTimeField(default=current_datetime)
 

if you figure out how is it done, a PR is welcomed to add it as django_jalali.utils.timzeone

@omidshojaee
Copy link
Author

how about

from django.utils import timezone
publish = jmodels.jDateTimeField(default=jdatetime.datetime.now(tz=timezone.get_current_timezone()))

@slashmili
Copy link
Owner

Looks good!

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

No branches or pull requests

3 participants