1

Am not able to figure out what mistake am doing

I know this question has been asked have gone with them but still am facing these errors

from datetime import datetime

class Images(models.Model):
  created_date = models.IntegerField(default=datetime.now().date())
  created_time = models.IntegerField(default=datetime.now().time())
  road_name = models.CharField(max_length = 100, default = 'NULL')

error with the above code am getting

Applying images.0002_auto_20200109_0728...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/usr/local/lib/python3.5/dist-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 "/usr/local/lib/python3.5/dist-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 "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
    new_db_params, strict,
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 648, in _alter_field
    old_default = self.effective_default(old_field)
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1429, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1408, in get_prep_value
    value = super().get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1393, in to_python
    params={'value': value},
django.core.exceptions.ValidationError: ["'07:28:18' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

and when I use Django core DateTimeField then I use to get stuck in auto_now_add and auto_now field

class Images(models.Model):
  created_date = models.DateTimeField(auto_now_add=True, default=timezone.now())
  created_time = models.TimeField(auto_now=True, default=timezone.now())

error

SystemCheckError: System check identified some issues:

ERRORS:
images.PotholeImages.created_date: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.
images.PotholeImages.created_time: (fields.E160) The options auto_now, auto_now_add, and default are mutually exclusive. Only one of these options may be present.

Thanks

Pradyum Gupta
  • 197
  • 1
  • 3
  • 21

1 Answers1

3

I strongly advise not to use IntegerFields, but use a DateTimeField [Django-doc] here. You can pass True to the auto_now_add=… parameter [Django-doc] to automatically set the current time:

class MyModel(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    road_name = models.CharField(max_length=100)

Not only will this set the current time when you create a new MyModel record, it will furthermore make the field non-editable (set the editable=… parameter [Django-doc] to False), and set the blank=… parameter [Django-doc] to True.

Please do not split a DateTimeField in two individual fields (a DateField and a TimeField). Time turns out to be "date-sensitive". Indeed, timezones and daylight saving time means that certain dates perform "shifts" on the time.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks for fast reply, but am still facing error with auto_now_add, could you please look into it – Pradyum Gupta Jan 09 '20 at 09:02
  • @PradyumGupta: but you should *remove* the `default=...` paramete.r This is handled by the `auto_now_add`. – Willem Van Onsem Jan 09 '20 at 09:04
  • I have used ```created_date = models.DateTimeField(auto_now_add=True)``` for time ```created_time = models.DateTimeField(auto_now_add=True)``` still error persist ```django.core.exceptions.ValidationError: ["'07:28:18' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]``` – Pradyum Gupta Jan 09 '20 at 09:12
  • @PradyumGupta: it does not make much sense to use two fields. Furthermore you likely have an extra field somwhere that is raising the error, or did not migrate your models. – Willem Van Onsem Jan 09 '20 at 09:15
  • 1
    Thank you there were issues in migrations, Somehow i was trying to figure out how should i get only time and date in two different fields – Pradyum Gupta Jan 09 '20 at 09:37