Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Im learning django-orm my code is this:

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)


and im creating a shell then im instantiation objects like this:
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()


But when i try to .save() my objects it's giving this error:
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


What I have tried:

I tried not to use timezone, but nothing changed
Posted
Updated 19-Jul-20 12:39pm
v2

1 solution

Okay someone helped me.
>> author = Author(name="Victor Hugo",created = timezone.now) 

timezone.now
should be
timezone.now()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900