0

I am new to django and i am creating my first project. I created one app in which model i define one postgres datatime field but i always get error for this field when run migrate command.

I used below value for datetime field in models.py but nothing happened

created=models.DateTimeField(default=timezone.now)
created=models.DateTimeField(default='', blank=True, null=True)
created=models.DateTimeField(blank=True, null=True)
created=models.DateTimeField(default='0000-00-00 00:00:00', blank=True, null=True)

in my settings.py

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

my models.py

from django.db import models
from django.utils import timezone
# from django.contrib.auth.models import User

# Create your models here.
class Operator(models.Model):
operator_code=models.CharField(unique=True, max_length=20)
operator_name=models.CharField(max_length=60)
operator_type=models.CharField(max_length=60)
short_code=models.CharField(max_length=60)
apiworld_code=models.CharField(max_length=20)
apiworld_operatortype=models.CharField(max_length=20)
active_api_id=models.IntegerField(default=1)
ospl_commission=models.DecimalField(default=0.00, max_digits=11, decimal_places=2)
apiworld_commission=models.DecimalField(default=0.00, max_digits=11, decimal_places=2)
image=models.CharField(max_length=100)
status=models.SmallIntegerField(default=1)
updated=models.DateTimeField(default=timezone.now)
created=models.DateTimeField(default=timezone.now)

def __str__(self):
    return self.operator_code

when i run 'python manage.py migrate' i got below error how to remove this

'django.core.exceptions.ValidationError: ["'0000-00-00 00:00:00' value has the correct format (YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ]) but it is an invalid date/time."]'

Dinesh Gurjar
  • 498
  • 5
  • 17

2 Answers2

6

To answer your question, the format for DateTimeField's in Django is defined in the default settings, as documented here: https://docs.djangoproject.com/en/2.2/ref/settings/#datetime-input-formats

At this point in time it defaults to the following, though obviously may differ depending on the version of Django you're using:

[
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
]

Looking at your code, however, I think you want to know the answer to "what is the correct way to create a default DateTime value in Django."

The answer to that depends on what you want that default to be. If you want the value to be null then it's easy.

myfield = models.DateTimeField(null=True, blank=True, default=None)

If you're trying to create created_at and updated_at fields then you want to use the auto_now_add and auto_now kwargs respectively, which will automatically fill out the fields with the time the model instance was created and updated.

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
AzMoo
  • 486
  • 3
  • 10
  • i created model according you but also get same error. then i Deleted the record with app="my-app-name" from that table (delete from django_migrations where app = "app-name"). Deleted my migration folder and run python manage.py makemigration my-app-name, then python manage.py migrate my-app-name. then this one is run with no error. thanks – Dinesh Gurjar Apr 24 '19 at 12:10
  • reference https://stackoverflow.com/questions/25958708/django-1-7-no-migrations-to-apply-when-run-migrate-after-makemigrations – Dinesh Gurjar Apr 24 '19 at 12:13
  • I have one question on Datetime format while storing in mysql, if we want to store 'YYYY-mm-dd hh:mm: ss' format using the model_fields(default site_packages folder in Django) file in site_packages how we can achieve? – Nithish Jul 16 '19 at 11:25
1

I have the same format error, when I was updateing my model.py

I show you my scenerio and maybe it will help. So I had an existing class and want to add this line:

time = models.TimeField()

than I did makemigrations and now the important part

Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value
for this column)
2) Quit, and let me add a default in models.py

choose 1) and type timezone.now for setting the default value

migrate and you got it