I have strange behavor in code.
My model is:
class Item(models.Model):
....
price = models.DecimalField(max_digits=5, decimal_places=2, help_text="Цена товара", verbose_name="Цена")
# price = models.IntegerField(help_text="Цена товара", verbose_name="Цена")
When I try filter object:
class ProductBuyView(APIView):
def get(self, request, item_uuid: str, format=None):
product = Item.objects.all().filter(uuid__exact=uuid.UUID(item_uuid))[0]
...
I get an exception
File "/home/lev/python_projects/taste_case_stripe/stripe_app/views.py", line 14, in get
product = Item.objects.all().filter(uuid__exact=uuid.UUID(item_uuid))[0]
File "/home/lev/python_projects/taste_case_stripe/venv/lib/python3.10/site-packages/django/db/models/query.py", line 445, in __getitem__
qs._fetch_all()
File "/home/lev/python_projects/taste_case_stripe/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1866, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/lev/python_projects/taste_case_stripe/venv/lib/python3.10/site-packages/django/db/models/query.py", line 117, in __iter__
for row in compiler.results_iter(results):
File "/home/lev/python_projects/taste_case_stripe/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1333, in apply_converters
value = converter(value, expression, connection)
File "/home/lev/python_projects/taste_case_stripe/venv/lib/python3.10/site-packages/django/db/backends/sqlite3/operations.py", line 344, in converter
return create_decimal(value).quantize(
decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
...decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
but when I change type field to Int
class Item(models.Model):
....
price = models.IntegerField(help_text="Цена товара", verbose_name="Цена")
everthing is OK. As I guess the problem is decimal field. But what is wrong with it...
CodePudding user response:
try this:
from django.db import models
from django.db.models import Model
# Create your models here.
class DecimalModel(Model):
geeks_field = models.DecimalField(
max_digits = 5,
decimal_places = 2)
in view:
from geeks.models import DecimalModel
# importing datetime module
import decimal
# creating an instance of
# datetime.date
d = decimal.Decimal(9.53)
# creating an instance of
# DecimalModel
my_object = DecimalModel.objects.create(geeks_field = d)
my_object.save()
CodePudding user response:
I guess there is some problem with data in your database. Can you delete all data from your Item
table? Problem should go away after this. As i see you use SQLight so you can copy db file as backup and try Item
table clean to diagnose the source of problem.