Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - TypeError: expected string or buffer

Tags:

python

django

Hello I added a field to a model and when I try to do migrate in terminal it give me this error, does anyone know what this might be?

=========================================================================

  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/schema.py", line 454, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 199, in _alter_field
    self._remake_table(model, alter_fields=[(old_field, new_field)])
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 137, in _remake_table
    self.create_model(temp_model)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/schema.py", line 212, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/schema.py", line 124, in column_sql
    default_value = self.effective_default(field)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/backends/schema.py", line 187, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 627, in get_db_prep_save
    prepared=False)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1290, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1269, in get_prep_value
    value = super(DateTimeField, self).get_prep_value(value)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1171, in get_prep_value
    return self.to_python(value)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1228, in to_python
    parsed = parse_datetime(value)
  File "/Users/Derek/.virtualenvs/tasks/lib/python2.7/site-packages/django/utils/dateparse.py", line 70, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or buffer

model.py

class Lead(models.Model):
    title = models.CharField(choices=TITLE_CHOICES, max_length=20, blank=True, default='')
    first_name = models.CharField(max_length=80, blank=True, default='')
    last_name = models.CharField(max_length=80, blank=True)
    company = models.CharField(max_length=80, blank=True)
    position = models.CharField(max_length=50, blank=True, default='')
    location = models.CharField(max_length=100, blank=True)
    currently_using_sms = models.CharField(choices=CURRENTLY_USING_SMS, default='',  max_length=30)
    services_interested_in = models.ManyToManyField(Services)
    phone_number = models.CharField(blank=True, max_length=20, default='')
    mobile_number = models.CharField(blank=True, max_length=11, default='')
    email = models.EmailField(blank='True', default='')
    best_day_to_contact = models.DateTimeField(blank=False, null=True)
    notes = models.TextField(blank=True)
    assign_to = models.ForeignKey(User, db_index=True, blank=True, null=True, default=None)
    lead_source = models.ForeignKey(LeadSource, db_index=True, blank=True, null=True)
    status = models.CharField(choices=LEAD_STATUS, max_length=100, default='1')
like image 353
nope Avatar asked Jun 15 '15 08:06

nope


3 Answers

Django’s DateTimeField generally works with Python datetime and date objects, but has an undocumented feature of being able to automatically parse strings into datetime objects. In your DateTimeField declaration, you had

default=False

which is neither datetime nor date. Thus, Django tries to parse it automatically for you. Failing to do so, it emits the cryptic message.

Depending on your specific need, you should either replace, or just remove the default=False argument.

like image 165
uranusjr Avatar answered Nov 15 '22 00:11

uranusjr


I believe that you have an error in default=False of the following line:

    completion_date = models.DateTimeField(blank=False, null=True, default=False)

False is not a valid date default expects. Use default=datetime.now instead.

like image 21
Yossi Avatar answered Nov 14 '22 23:11

Yossi


I have the same issue before. I tried default=False but it doesn't make any change. Then I add default=timezone.now, It works correctly. Only problem is date field are not displayed in admin section.

change this field:

best_day_to_contact = models.DateTimeField(blank=False, null=True)

to this field:

from django.utils import timezone
best_day_to_contact = models.DateTimeField(default=timezone.now)

I think maybe your problem will fix with this solution.

like image 45
Arun V Jose Avatar answered Nov 14 '22 23:11

Arun V Jose