cancaglar Ответов: 1

Питон-Джанго ошибку TypeError: ожидается строк или байт-ошибок, как объект-объект.сохранить()


Я изучаю 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)


и я создаю оболочку, а затем создаю экземпляры таких объектов, как этот:
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


Что я уже пробовал:

Я старался не использовать часовой пояс, но ничего не изменилось

1 Ответов

Рейтинг:
0

cancaglar

Ладно, кто-то мне помог.

>> author = Author(name="Victor Hugo",created = timezone.now) 

timezone.now
должно быть
timezone.now()