django 迁移抛出日期格式错误
django migration throws date format error
正在尝试使用以下方法进行迁移
models.py
from django.db import models
from django.utils.text import slugify
from django.utils import timezone
# Create your models here.
CATEGORY_CHOICES = (
('action','ACTION'),
('drama','DRAMA'),
('comedy','COMEDY'),
('romance','ROMANCE'),
)
LANGUAGE_CHOICES = (
('english' , 'ENGLISH'),
('german' , 'GERMAN'),
)
STATUS_CHOICES = (
('RA' , 'RECRNTLY ADDED'),
('MW' , 'MOST WATCHED'),
('TR' , 'TOP RATED'),
)
class Movie(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(max_length=1000)
image = models.ImageField(upload_to='movies')
banner = models.ImageField(upload_to='movies_banner')
category = models.CharField(choices=CATEGORY_CHOICES , max_length=10)
language = models.CharField(choices=LANGUAGE_CHOICES , max_length=10)
status = models.CharField(choices=STATUS_CHOICES , max_length=2)
cast = models.CharField(max_length=100)
year_of_production = models.DateField()
views_count = models.IntegerField(default=0)
movie_trailer = models.URLField()
created = models.DateTimeField(default=timezone.now)
slug = models.SlugField(blank=True, null=True)
def save(self , *args , **kwargs):
if not self.slug :
self.slug = slugify(self.title)
super(Movie , self).save( *args , **kwargs)
def __str__(self):
return self.title
LINK_CHOICES = (
('D' , 'DOWNLOAD LINK'),
('W' , 'WATCH LINK'),
)
class MovieLinks(models.Model):
movie = models.ForeignKey(Movie, related_name='movie_watch_link', on_delete=models.CASCADE)
type = models.CharField(choices=LINK_CHOICES , max_length=1)
link = models.URLField()
def __str__(self):
return str(self.movie)
输出:
django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']
完整输出:
PS C:\Users\zenmy\PycharmProjects\pythonProject\backup\imdb> python .\manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, movie, sessions
Running migrations:
Applying movie.0007_movie_created...Traceback (most recent call last):
File ".\manage.py", line 22, in <module>
main()
File ".\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 460, in execute
output = self.handle(*args, **options)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\migration.py", line 131, in apply
operation.database_forwards(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\operations\fields.py", line 108, in database_forwards
schema_editor.add_field(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 381, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 230, in _remake_table
self.effective_default(create_field)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\schema.py", line 410, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 910, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1546, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1524, in get_prep_value
value = super().get_prep_value(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1403, in get_prep_value
return self.to_python(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1506, in to_python
raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']
在此处找到解决方案
[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
必须删除迁移下的所有内容,然后
created = models.DateTimeField(default=timezone.now)
必须设置为 blank=True
只需将创建的字段更改为
timezone.now 错误替换为 timezone.now()
created = models.DateTimeField(default=timezone.now())
及之后
python manage.py makemigrations
python manage.py migrate
正在尝试使用以下方法进行迁移
models.py
from django.db import models
from django.utils.text import slugify
from django.utils import timezone
# Create your models here.
CATEGORY_CHOICES = (
('action','ACTION'),
('drama','DRAMA'),
('comedy','COMEDY'),
('romance','ROMANCE'),
)
LANGUAGE_CHOICES = (
('english' , 'ENGLISH'),
('german' , 'GERMAN'),
)
STATUS_CHOICES = (
('RA' , 'RECRNTLY ADDED'),
('MW' , 'MOST WATCHED'),
('TR' , 'TOP RATED'),
)
class Movie(models.Model):
title = models.CharField(max_length=100)
description = models.TextField(max_length=1000)
image = models.ImageField(upload_to='movies')
banner = models.ImageField(upload_to='movies_banner')
category = models.CharField(choices=CATEGORY_CHOICES , max_length=10)
language = models.CharField(choices=LANGUAGE_CHOICES , max_length=10)
status = models.CharField(choices=STATUS_CHOICES , max_length=2)
cast = models.CharField(max_length=100)
year_of_production = models.DateField()
views_count = models.IntegerField(default=0)
movie_trailer = models.URLField()
created = models.DateTimeField(default=timezone.now)
slug = models.SlugField(blank=True, null=True)
def save(self , *args , **kwargs):
if not self.slug :
self.slug = slugify(self.title)
super(Movie , self).save( *args , **kwargs)
def __str__(self):
return self.title
LINK_CHOICES = (
('D' , 'DOWNLOAD LINK'),
('W' , 'WATCH LINK'),
)
class MovieLinks(models.Model):
movie = models.ForeignKey(Movie, related_name='movie_watch_link', on_delete=models.CASCADE)
type = models.CharField(choices=LINK_CHOICES , max_length=1)
link = models.URLField()
def __str__(self):
return str(self.movie)
输出:
django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']
完整输出:
PS C:\Users\zenmy\PycharmProjects\pythonProject\backup\imdb> python .\manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, movie, sessions
Running migrations:
Applying movie.0007_movie_created...Traceback (most recent call last):
File ".\manage.py", line 22, in <module>
main()
File ".\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 460, in execute
output = self.handle(*args, **options)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\commands\migrate.py", line 290, in handle
post_migrate_state = executor.migrate(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 131, in migrate
state = self._migrate_all_forwards(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 163, in _migrate_all_forwards
state = self.apply_migration(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\executor.py", line 248, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\migration.py", line 131, in apply
operation.database_forwards(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\migrations\operations\fields.py", line 108, in database_forwards
schema_editor.add_field(
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 381, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\sqlite3\schema.py", line 230, in _remake_table
self.effective_default(create_field)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\backends\base\schema.py", line 410, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 910, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1546, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1524, in get_prep_value
value = super().get_prep_value(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1403, in get_prep_value
return self.to_python(value)
File "C:\Users\zenmy\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1506, in to_python
raise exceptions.ValidationError(
django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.']
在此处找到解决方案
[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
必须删除迁移下的所有内容,然后
created = models.DateTimeField(default=timezone.now)
必须设置为 blank=True
只需将创建的字段更改为 timezone.now 错误替换为 timezone.now()
created = models.DateTimeField(default=timezone.now())
及之后
python manage.py makemigrations
python manage.py migrate