Negrita Developers
Negrita Developers

Reputation: 39

Migration error. TypeError: expected string or bytes-like object django

I have a problem, this gives me an error and I don't know why. I have tried everything including deleting the date fields even if I change it to a datetime field, i always get the same error. This is django 1.11. This happens everytime I migrate.

1.This is the traceback of the error.

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
    

I also tried adding auto_now_add.

  1. Migration: 0031_auto_20181107_1420.py

class Migration(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'),
    ),
]

Upvotes: 1

Views: 2945

Answers (3)

user10449598
user10449598

Reputation:

Late reply but yes I encountered the same issue. I removed the

migrations folder

python manage.py makemigrations app_name
python manage.py migrate

It worked for me.

Upvotes: 4

Saad Mirza
Saad Mirza

Reputation: 1187

Delete Your Recent Migrations, Then after creating one migration add this

from django.utils import timezone

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

Upvotes: 0

Alasdair
Alasdair

Reputation: 309109

Your migration is trying to add dates with default=0, which is not a valid date.

field=models.DateField(default=0),

You could fix the migration to use a real date, for example:

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

Because you have changed the models since you created the migration, it would probably be safer to delete the migration 0031_auto_20181107_1420 (make a backup first), fix the models, then rerun makemigrations to create a valid migration file.

If you add null=True when adding a date/datetime field, then you won't have to specify a default. Otherwise, you will have to specify a default either in your model field or in the migration.

Upvotes: 0

Related Questions