Reputation: 180
I'm 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 I'm creating a shell then I'm 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
Any help? What I'm supposed to do? I'm pretty new here and this is my first question
Upvotes: 2
Views: 648
Reputation: 258
The issue is depicted on this line of the error message:
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
It indicates that your field for your datetime (when you created an Author object with timezone.now
) was invalid.
This is because you didn't call it correctly. Your shell should show 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()
Notice the change in timezone.now
to timezone.now()
.
Here's a link to the reasoning behind the answer.
Upvotes: 1