django - TypeError:期望的字符串或缓冲区

时间:2015-06-15 08:26:08

标签: python django

您好我在模型中添加了一个字段,当我尝试在终端中进行迁移时,它会给我这个错误,有人知道这可能是什么吗?

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

  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')

6 个答案:

答案 0 :(得分:9)

Django的DateTimeField通常适用于Python datetimedate个对象,但has an undocumented feature of being able to automatically parse strings into datetime objects。在您的DateTimeField声明中,您有

default=False

既不是datetime也不是date。因此,Django会尝试自动为您解析它。如果不这样做,就会发出神秘的信息。

根据您的具体需要,您应该替换,或者只删除default=False参数。

答案 1 :(得分:1)

我相信您在以下行的default=False中出错:

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

False不是有效日期default。请改用default=datetime.now

答案 2 :(得分:1)

我之前有同样的问题。我试过default = False,但它没有做任何改变。然后我添加default = timezone.now,它正常工作。唯一的问题是日期字段不会显示在管理部分中。

更改此字段:

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

到这个领域:

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

我想也许你的问题会解决这个问题。

答案 3 :(得分:1)

/* Example Model */     

class user(models.Model):
    created = models.DateField(auto_now_add=True)
    def __str__(self):
        return str(self.id)

/*When you generate migrations file then it will add (default=1)
*Remove default=1
*field=models.DateField(default=1, auto_now_add=True),
*To 
*field=models.DateField(auto_now_add=True)

*Then do sqlmigrate or migrate.  
*/

答案 4 :(得分:0)

只是想权衡一下,因为这个问题使我非常头疼。在我添加了models.DateField之后,Django对我来说是一个糟糕的迁移。直到我删除了所有迁移并重新进行makemigrations后,它才能自行修复。

希望这会有所帮助!

答案 5 :(得分:0)

要解决此问题,可能需要您返回到文本编辑器并删除所有迁移。当django迁徙不佳时,通常会发生这种情况。之后,重新进行新的迁移。希望这能解决您的问题