迁移错误。 TypeError:预期的字符串或类似字节的对象django

时间:2018-11-07 17:10:04

标签: python django

我有问题,这给我一个错误,我不知道为什么。我已经尝试了所有操作,包括删除日期字段,即使将其更改为datetime字段,我也总是遇到相同的错误。这是Django 1.11。每当我迁移时都会发生这种情况。

1。这是错误的回溯。

python manage.py migrate

Operations to perform:
Apply all migrations: admin, auth, contenttypes, profiles, sessions
Running migrations:
Applying profiles.0031_auto_20181107_1420...Traceback (most recent 
call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/claudia/Documents/kiboko/Aramati- 
Safaris/virtual/lib/python3.5/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/home/claudia/Documents/kiboko/Aramati-Safaris/virtual/lib/python3.5/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/home/claudia/Documents/kiboko/Aramati-Safaris/virtual/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
 File "/home/claudia/Documents/kiboko/Aramati-Safaris/virtual/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
 File "/home/claudia/Documents/kiboko/Aramati-Safaris/virtual/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
 File "/home/claudia/Documents/kiboko/Aramati-Safaris/virtual/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
 File "/home/claudia/Documents/kiboko/Aramati- 
Safaris/virtual/lib/python3.5/site- 
packages/django/db/migrations/executor.py", line 145, in 
_migrate_all_forwards
 state = self.apply_migration(state, migration, fake=fake, 
fake_initial=fake_initial)
File "/home/claudia/Documents/kiboko/Aramati- 
Safaris/virtual/lib/python3.5/site- 
 packages/django/db/migrations/executor.py", line 244, in 
apply_migration
 state = migration.apply(state, schema_editor)
 File "/home/claudia/Documents/kiboko/Aramati- 
Safaris/virtual/lib/python3.5/site- 
packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, 
 project_state)
 File "/home/claudia/Documents/kiboko/Aramati- 
Safaris/virtual/lib/python3.5/site- 
packages/django/db/migrations/operations/fields.py", line 86, in 
 database_forwards
 field,
  File "/home/claudia/Documents/kiboko/Aramati- 
 Safaris/virtual/lib/python3.5/site- 
 packages/django/db/backends/base/schema.py", line 414, in add_field
 definition, params = self.column_sql(model, field,         
 include_default=True)
 File "/home/claudia/Documents/kiboko/Aramati- 
 Safaris/virtual/lib/python3.5/site- 
 packages/django/db/backends/base/schema.py", line 154, in column_sql
 default_value = self.effective_default(field)
 File "/home/claudia/Documents/kiboko/Aramati- 
 Safaris/virtual/lib/python3.5/site- 
 packages/django/db/backends/base/schema.py", line 228, in 
 effective_default
  default = field.get_db_prep_save(default, self.connection)
 File "/home/claudia/Documents/kiboko/Aramati- 
 Safaris/virtual/lib/python3.5/site- 
  packages/django/db/models/fields/__init__.py", line 766, in 
 get_db_prep_save
  prepared=False)
  File "/home/claudia/Documents/kiboko/Aramati- 
 Safaris/virtual/lib/python3.5/site- 
 packages/django/db/models/fields/__init__.py", line 1297, in 
  get_db_prep_value
   value = self.get_prep_value(value)
  File "/home/claudia/Documents/kiboko/Aramati- 
  Safaris/virtual/lib/python3.5/site- 
  packages/django/db/models/fields/__init__.py", line 1292, in 
  get_prep_value
  return self.to_python(value)
   File "/home/claudia/Documents/kiboko/Aramati- 
   Safaris/virtual/lib/python3.5/site- 
  packages/django/db/models/fields/__init__.py", line 1254, in 
  to_python
  parsed = parse_date(value)
  File "/home/claudia/Documents/kiboko/Aramati- 
  Safaris/virtual/lib/python3.5/site- 
  packages/django/utils/dateparse.py", 
  line 61, in parse_date
  match = date_re.match(value)
  TypeError: expected string or bytes-like object
  1. models.py

    class GuaranteedSafaris(models.Model):
    
    SafariPackagesChoices=(
    ('Excursions-lakenakuru', 'Excursions-lakenakuru'),
    ('Excursions-lakenaivasha' ,'Excursions-lakenaivasha'),
    ('Excursions-nairobiexcursions' ,'Excursions-nairobiexcursions'),
    ('2dyabedare' ,'2 days - aberdare'),
    ('2dyamboseli' ,'2 days - amboseli'),
    ('2dynakuru' ,'2 days - nakuru'),
    ('2dynaivasha' ,'2 days - naivasha'),
    ('2dysweetwaters' ,'2 days - sweetwaters'),
    ('3dyamboseli' ,'3 days - amboseli'),
    ('3dymaasaimara' ,'3 days - maasaimara '),
    ('3dynakuru' ,'3 days - nakuru'),
    ('3dysamburu' ,'3 days - samburu'),
    ('3dysweetwaters' ,'3 days - sweetwaters'),
    ('4dymaranakuru' ,'4 days - maranakuru'),
    ('5dyamboseli' ,'5 days - amboseli'),
    
    )
     Enter_Safari_Package=models.CharField(choices= 
     SafariPackagesChoices, 
     max_length=50, default="title")
    
     book_before=models.DateTimeField()
     depature_date=models.DateTimeField()
    
     Accomodation=(
    ('bg', 'Budget'),
    ('lx','Luxury')
     )
    Accomodation=models.CharField(choices=Accomodation, max_length=20, 
    default="accomodation")
    
     people_booked=models.IntegerField(default=0)
    
     def __str__(self):
    return self.Enter_Safari_Package
    

我还尝试添加auto_now_add。

  1. 迁移:0031_auto_20181107_1420.py

类迁移(migrations.Migration):

dependencies = [
    ('profiles', '0030_auto_20181107_0102'),
]

operations = [
    migrations.CreateModel(
        name='LowSeason',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('price1', models.IntegerField(default=0)),
            ('price2', models.IntegerField(default=0)),
            ('price3', models.IntegerField(default=0)),
            ('price4', models.IntegerField(default=0)),
            ('price5', models.IntegerField(default=0)),
            ('price6', models.IntegerField(default=0)),
            ('price7', models.IntegerField(default=0)),
        ],
    ),
    migrations.CreateModel(
        name='Packages',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('name', models.CharField(default='package name', max_length=30)),
        ],
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='Enter_Hotel_Star',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price1',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price2',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price3',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price4',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price5',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price6',
    ),
    migrations.RemoveField(
        model_name='guaranteedsafaris',
        name='price7',
    ),
    migrations.AddField(
        model_name='guaranteedsafaris',
        name='Accomodation',
        field=models.CharField(choices=[('bg', 'Budget'), ('lx', 'Luxury')], default='accomodation', max_length=20),
    ),
    migrations.AddField(
        model_name='guaranteedsafaris',
        name='depature_date',
        field=models.DateField(default=0),
    ),
    migrations.AlterField(
        model_name='guaranteedsafaris',
        name='book_before',
        field=models.DateField(default=0),
    ),
    migrations.AddField(
        model_name='lowseason',
        name='package',
        field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='profiles.Packages'),
    ),
]

3 个答案:

答案 0 :(得分:0)

您的迁移正在尝试使用default=0添加日期,这不是有效日期。

field=models.DateField(default=0),

您可以修复迁移以使用实际日期的情况,例如:

migrations.AddField(
    model_name='guaranteedsafaris',
    name='depature_date',
    field=models.DateField(default=date.now()),
),

由于自从创建迁移以来已经更改了模型,因此删除迁移0031_auto_20181107_1420(首先进行备份),修复模型,然后重新运行makemigrations来创建迁移可能更安全。有效的迁移文件。

如果在添加日期/日期时间字段时添加null=True,则无需指定默认值。否则,您将必须在模型字段或迁移中指定默认值。

答案 1 :(得分:0)

删除您最近的迁移, 然后在创建一次迁移后添加

from django.utils import timezone

date_created = models.DateTimeField('date_created', default=timezone.now(), blank=False)

答案 2 :(得分:0)

最新答复,但是是的,我遇到了同样的问题。 我删除了

  

迁移文件夹

python manage.py makemigrations app_name
python manage.py migrate

对我有用。