This simple model is an example
from django.db import models
# Create your models here.
class Bonsai(models.Model):
# remove (s) from table name in Django admin
class Meta:
verbose_name_plural = "Bonsai"
name = models.CharField(max_length=64)
age = models.IntegerField()
placement = models.CharField(max_length=64)
def __str__(self):
return f"{self.name} ({self.age} years)"
o = Bonsai(“Oak”, 44,“Outdoor”)
o.save()
Traceback…
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\base.py", line 812, in save
self.save_base(
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\base.py", line 863, in save_base
updated = self._save_table(
^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\base.py", line 976, in _save_table
updated = self._do_update(
^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\base.py", line 1019, in _do_update
filtered = base_qs.filter(pk=pk_val)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\query.py", line 1420, in filter
return self._filter_or_exclude(False, args, kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\query.py", line 1438, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\query.py", line 1445, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\sql\query.py", line 1532, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\sql\query.py", line 1562, in _add_q
child_clause, needed_inner = self.build_filter(
^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\sql\query.py", line 1478, in build_filter
condition = self.build_lookup(lookups, col, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\sql\query.py", line 1303, in build_lookup
lookup = lookup_class(lhs, rhs)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\lookups.py", line 27, in __init__
self.rhs = self.get_prep_lookup()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\lookups.py", line 341, in get_prep_lookup
return super().get_prep_lookup()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Studio\Desktop\CodeMedia\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2020, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'Oak'.