Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

python - Django migrate error : TypeError expected string or bytes-like object

I'm trying to learn django and error occur in changing models. I tried a lot like default=datetime.datetime.now but I dont know how to fix it.

these are my models

from django.db import models import datetime

class Candidate(models.Model):
    name = models.CharField(max_length=10)
    introduction = models.TextField()
    area = models.CharField(max_length=15)
    party_number=models.IntegerField(default=0)
    def __str__(self) :
        return self.name

class Poll(models.Model) :
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    area = models.CharField(max_length=15)

class Choice(models.Model) :
    poll = models.ForeignKey(Poll)
    candidate = models.ForeignKey(Candidate)
    votes = models.IntegerField(default=0)

when I type commmand : python manage.py migrate, this error occured

Operations to perform:
  Apply all migrations: admin, ang, auth, contenttypes, sessions
Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:PythonPython35-32libsite-packagesdjangocoremanagement\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:PythonPython35-32libsite-packagesdjangocoremanagement\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:PythonPython35-32libsite-packagesdjangocoremanagementase.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:PythonPython35-32libsite-packagesdjangocoremanagementase.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:PythonPython35-32libsite-packagesdjangocoremanagementcommandsmigrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:PythonPython35-32libsite-packagesdjangodbmigrationsexecutor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:PythonPython35-32libsite-packagesdjangodbmigrationsexecutor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:PythonPython35-32libsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:PythonPython35-32libsite-packagesdjangodbmigrationsmigration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:PythonPython35-32libsite-packagesdjangodbmigrationsoperationsfields.py", line 84, in database_forwards
    field,
  File "C:PythonPython35-32libsite-packagesdjangodbackendssqlite3schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:PythonPython35-32libsite-packagesdjangodbackendssqlite3schema.py", line 113, in _remake_table
    self.effective_default(field)
  File "C:PythonPython35-32libsite-packagesdjangodbackendsaseschema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:PythonPython35-32libsite-packagesdjangodbmodelsfields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:PythonPython35-32libsite-packagesdjangodbmodelsfields\__init__.py", line 1438, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:PythonPython35-32libsite-packagesdjangodbmodelsfields\__init__.py", line 1417, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:PythonPython35-32libsite-packagesdjangodbmodelsfields\__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "C:PythonPython35-32libsite-packagesdjangodbmodelsfields\__init__.py", line 1378, in to_python
    parsed = parse_datetime(value)
  File "C:PythonPython35-32libsite-packagesdjangoutilsdateparse.py", line 93, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

Please help me!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you modified the fields in models. after that you run makemigrations that time it asking like this

^C(api_env)nyros@nyros:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1

We select 1 option, then it will display like this

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

We given timezone.now() then makemigrations completed.

See your migrations last file, there is

class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('providers', '0023_remove_content_provider'),
]

operations = [
    migrations.AddField(
        model_name='content',
        name='provider',
        field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
        preserve_default=False,
    ),
]

In above code observe this line

default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)

In that line forgeinkey field default value is datetime, Is it correct value?

No, so you have to give some string or object is a default value to that field.

Now you have to edit that value in that corresponding migration file, like this

default='some string'

then save and run the migrate command.

Try and let me know, Is it works or not.Thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...