slashmili / django-jalali

Jalali DateField support for Django model
http://pypi.python.org/pypi/django_jalali
BSD 3-Clause "New" or "Revised" License
258 stars 52 forks source link

default = datetime.now() results in gregorian date #109

Closed typerhack closed 4 years ago

typerhack commented 4 years ago

Greetings, I used jDateTimeFeild as follows: article_persian_date = jmodels.jDateTimeField(default=datetime.now())

I think at first it automatically converted it to Jalali date since the package checks for "datetime" instance. but now it shows the default as a gregorian date. I checked the package if I use "auto_add_now = True" this will make the field not editable. I think many want to add now automatically and still be able to edit the time.

image

If I use "jDateTime.datetime.now()", while making migrations, i get the error it cannot serialize.

hramezani commented 4 years ago

Hi @typerhack, Thanks for reporting this.

If I use "jDateTime.datetime.now()", while making migrations, i get the error it cannot serialize.

Yes, you are right. that's because jdatetime.datetime is not an instance of datetime.datetime:

>>> import datetime
>>> import jdatetime
>>> d = jdatetime.datetime.now()
>>> isinstance(d, datetime.datetime)
False

The solution would be set a Jalali datetime which is an instance of datetime.datetime as default:

article_persian_date = jmodels.jDateTimeField(default=datetime.fromisoformat(jdatetime.datetime.now().isoformat()))

Are you interested in creating a PR and add this to README?

typerhack commented 4 years ago

Thanks for the response I would surely like to create a PR. However, the issue has not been resolved yet. I copied the code in your answer it is still getting this message:

Operations to perform: Apply all migrations: admin, auth, author, banner, blog, contenttypes, sessions Running migrations: D:\DjangoApp\venv\lib\site-packages\django\db\models\fields\__init__.py:1368: RuntimeWarning: DateTimeField Author.start_date received a naive datetime (2020-05-16 08:42:18.553453) while time zone support is active. RuntimeWarning) D:\DjangoApp\venv\lib\site-packages\django\db\models\fields\__init__.py:1368: RuntimeWarning: DateTimeField Author.start_date received a naive datetime (2020-05-16 18:06:04.609582) while time zone support is active. RuntimeWarning) D:\DjangoApp\venv\lib\site-packages\django_jalali\db\models.py:321: RuntimeWarning: DateTimeField Author.start_persian_date received a naive datetime (1399-02-27 08:42:18.553453) while time zone support is active. RuntimeWarning Applying author.0004_auto_20200516_1806...Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "D:\DjangoApp\venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line utility.execute() File "D:\DjangoApp\venv\lib\site-packages\django\core\management\__init__.py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "D:\DjangoApp\venv\lib\site-packages\django\core\management\base.py", line 328, in run_from_argv self.execute(*args, **cmd_options) File "D:\DjangoApp\venv\lib\site-packages\django\core\management\base.py", line 369, in execute output = self.handle(*args, **options) File "D:\DjangoApp\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "D:\DjangoApp\venv\lib\site-packages\django\core\management\commands\migrate.py", line 233, in handle fake_initial=fake_initial, File "D:\DjangoApp\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "D:\DjangoApp\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "D:\DjangoApp\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration state = migration.apply(state, schema_editor) File "D:\DjangoApp\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "D:\DjangoApp\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "D:\DjangoApp\venv\lib\site-packages\django\db\backends\base\schema.py", line 565, in alter_field old_db_params, new_db_params, strict) File "D:\DjangoApp\venv\lib\site-packages\django\db\backends\postgresql\schema.py", line 154, in _alter_field new_db_params, strict, File "D:\DjangoApp\venv\lib\site-packages\django\db\backends\base\schema.py", line 679, in _alter_field new_default = self.effective_default(new_field) File "D:\DjangoApp\venv\lib\site-packages\django\db\backends\base\schema.py", line 303, in effective_default return field.get_db_prep_save(self._effective_default(field), self.connection) File "D:\DjangoApp\venv\lib\site-packages\django\db\models\fields\__init__.py", line 821, in get_db_prep_save return self.get_db_prep_value(value, connection=connection, prepared=False) File "D:\DjangoApp\venv\lib\site-packages\django_jalali\db\models.py", line 330, in get_db_prep_value value = self.get_prep_value(value) File "D:\DjangoApp\venv\lib\site-packages\django_jalali\db\models.py", line 311, in get_prep_value value = self.to_python(value) File "D:\DjangoApp\venv\lib\site-packages\django_jalali\db\models.py", line 300, in to_python return self.parse_date(value) File "D:\DjangoApp\venv\lib\site-packages\django_jalali\db\models.py", line 281, in parse_date raise exceptions.ValidationError(self.error_messages['invalid']) django.core.exceptions.ValidationError: ['Enter a valid date/time in YYYY-MM-DD HH:MM[:ss[.uuuuuu]] format.']

It seems like it has an issue with parsing the date now.

hramezani commented 4 years ago

Is it a new project? or you are going to change the existing project? it would be good to create a simple project with the mentioned error. You can push it to Github or send me the project zip file by email: hasan.r67@gmail.com Then I can investigate it. Thanks

typerhack commented 4 years ago

It is an existing project. I'll create a new one and send it to you

typerhack commented 4 years ago

I tested it on a test project and worked fine. It seems like it had an issue with my DB. I recreated the DB and everything is fine. I am going to create a PR. I also may add a new bootstrap picker to the module. I am not very familiar with module development but I'll try my best. Thank you

hramezani commented 4 years ago

Nice! I think it would be better to handle this conversion in JDateTimefild.

typerhack commented 4 years ago

I must add that this approach only returns the start time of the server and it is not dynamic. to elaborate, we need the time to be updated to the current time. We do have this problem with datetime.now() too. by using datetime.now without parenthesis the dynamic time can be fixed. I guess it is better to convert from this to jdatetime

hramezani commented 4 years ago

I think setting a default value for JDateTimeField or DateTimeField is not the right way to have the current datetime in the admin panel. You can change the form initialization or saving. Please take a look at this Stackoverflow question.

Actually, the problem is setting a jdatetime.datetime instance as default value for JDateTimeField. something like:

jdt1 = jdatetime.datetime(1392, 3, 12, 10, 22, 23, 240000)
field = jmodels.jDateTimeField(default=jdt1)

which cause migration error as you mentioned in issue description.

The #112 will fix this problem.