【问题标题】:Python-django typeerror: expected string or bytes-like object error -- object.save()Python-django typeerror:预期的字符串或类似字节的对象错误——object.save()
【发布时间】:2020-11-09 03:06:32
【问题描述】:

我正在学习 django-orm 我的代码是这样的:

from django.db import models
# Create your models here.
class Author(models.Model):
    def __str__(self):
        return self.name
    name = models.CharField(max_length=50)
    created = models.DateTimeField() 
class Book(models.Model):
    def __str__(self):
        return self.name
    name = models.CharField(max_length=50) 
    created = models.DateTimeField()
    
    author = models.ForeignKey(Author, on_delete = models.CASCADE) 
    price = models.DecimalField(decimal_places=2, max_digits=4, null=True)

我正在创建一个 shell,然后我是这样的实例化对象:

python manage.py shell
   >> from books.models import Author,Book 
   >> Author.objects.all()
   >> from django.utils import timezone
   >> author = Author(name="Victor Hugo",created = timezone.now) 
   >> author.save()

但是当我尝试 .save() 我的对象时,它给出了这个错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 740, in save     
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 777, in save_base
    updated = self._save_table(
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 870, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\base.py", line 907, in _do_insert
    return manager._insert([self], fields=fields, return_id=update_pk,
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 1186, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1334, in execute_sql
    for sql, params in self.as_sql():
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1276, in as_sql
    value_rows = [
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1277, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1277, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\compiler.py", line 1218, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1431, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1410, in get_prep_value
    value = super().get_prep_value(value)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1270, in get_prep_value
    return self.to_python(value)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1371, in to_python
    parsed = parse_datetime(value)
  File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime
    match = datetime_re.match(value)
TypeError: expected string or bytes-like object

有什么帮助吗?我该怎么办?我是新手,这是我的第一个问题

【问题讨论】:

    标签: python python-3.x django orm


    【解决方案1】:

    错误信息的这一行描述了问题:

      File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1371, in to_python
        parsed = parse_datetime(value)
      File "C:\Users\Casper\AppData\Local\Programs\Python\Python38\lib\site-packages\django\utils\dateparse.py", line 106, in parse_datetime
        match = datetime_re.match(value)
      TypeError: expected string or bytes-like object
    

    这表明您的日期时间字段(当您使用timezone.now 创建作者对象时)无效。

    这是因为你没有正确调用它。你的 shell 应该显示这个:

    python manage.py shell
       >> from books.models import Author,Book 
       >> Author.objects.all()
       >> from django.utils import timezone
       >> author = Author(name="Victor Hugo",created = timezone.now()) 
       >> author.save()
    

    注意 timezone.nowtimezone.now() 的变化。

    Here's a link to the reasoning behind the answer.

    【讨论】:

    • 天哪 -.- 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 2017-03-14
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2019-10-31
    相关资源
    最近更新 更多