Django porting error: expected string TypeError or byte-like object

I am trying to learn django and there are errors when changing the model. I've tried a lot like default = datetime.datetime.now, but I don't know how to fix it.

these are my models

from django.db import models import date / time

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:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "C:\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Python\Python35-32\lib\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 "C:\Python\Python35-32\lib\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 "C:\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Python\Python35-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 231, in add_field
    self._remake_table(model, create_fields=[field])
  File "C:\Python\Python35-32\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
    self.effective_default(field)
  File "C:\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 221, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 755, in get_db_prep_save
    prepared=False)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1438, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1417, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1275, in get_prep_value
    return self.to_python(value)
  File "C:\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 1378, in to_python
    parsed = parse_datetime(value)
  File "C:\Python\Python35-32\lib\site-packages\django\utils\dateparse.py", line 93, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

      

Please help me!!

+5


source to share


9 replies


If you have changed the fields in the models. after that you run makemigrations, this time 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 choose 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 provided timezone.now () and then the mocking was done.

Look at the latest migration 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 the above code, observe this line



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

      

In this line, the default forgeinkey is datetime, is the value correct?

No, so you need to provide some string or default object for this field.

Now you need to edit this value in the appropriate migration file, for example

default='some string'

      

then save and run the migrate command.

Give it a try and let me know if it works or not. thank

+5


source


Remove your migrations with these commands:

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

      



Then delete the database file using the command rm db.sqlite

Then start your server

+3


source


Change the default for the model field to auto_now_add=True

end_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(auto_now_add=True)

      

+2


source


Just delete all files in the migration folder except "init.py" and delete "db.sqlite3" in your database, now make the changes you want and migrate. Hope This helps :)

0


source


Same problem when starting with adding placeholders to my date fields, I accidentally entered "1" where it should be "django.utils.timezone.now" ie for the default date. Go to your last jump and find "default = xxx" which is not like the others and paste in "django.utils.timezone.now"

0


source


Go to folder migrations

" migrations

"

go to your migration file, for example "0054_comment_date.py"

change 'default'

in linefield=models.DateTimeField(auto_now_add=True, default=0)

from 0 or there is

by default='2012-09-04 06:00:00.000000-08:00

'

0


source


I faced the same problem. The solution was to remove all default spaces in the 'migrations' folder.

while you are typing 'python manage.py migrate' go to terminal and find exactly where the file in 'migrations' is stopping the migration. Then find all default arguments for DateField or DateTimeField and remove it. These models are not processed with a default argument.

migrations.AlterField(
        model_name='order',
        name='start_date',
        field=models.DateField(auto_now_add=True, ***BUG default=False BUG***),
        preserve_default=False,
    ),

      

See the log in your terminal:

Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):

      

there is an error in this file. Go there and remove the default argument from DateField

0


source


I faced the same problem. The solution was to remove all default spaces in the 'migrations' folder.

while you are typing 'python manage.py migrate' go to terminal and find exactly where the file in 'migrations' is stopping the migration. Then find all default arguments for DateField or DateTimeField and remove it. These models are not processed with a default argument.

Look at the log in your terminal: Running migrations: applying ang.0003_poll_end_date ... Traceback (last call was last):

there is an error in this file. Go there and remove the default argument from DateField

0


source


I had the same problem. Look at all the lines of models.DateTimeField in the .py file created after calling makemigrations. There you will notice that some of the DateTimeField has the wrong default. Replace them with models.DateTimeField (auto_now_add = True, default = django.utils.timezone.now), eg.

-2


source







All Articles