Tried To Follow This Issue and Still Not Solve It.
as i understood from the Error The Price in the model is a DecimalField and it is what make this Issue here.but when i change it to FloatField also i got same Error., Dont Know Why as I return context Dict in the in render function.
grateful for support guys.
Error Traceback
Traceback (most recent call last):
File "/home/amw/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/home/amw/.local/lib/python3.10/site-packages/django/utils/deprecation.py", line 138, in __call__
response = self.process_response(request, response)
File "/home/amw/.local/lib/python3.10/site-packages/django/contrib/sessions/middleware.py", line 59, in process_response
request.session.save()
File "/home/amw/.local/lib/python3.10/site-packages/django/contrib/sessions/backends/db.py", line 82, in save
obj = self.create_model_instance(data)
File "/home/amw/.local/lib/python3.10/site-packages/django/contrib/sessions/backends/db.py", line 69, in create_model_instance
session_data=self.encode(data),
File "/home/amw/.local/lib/python3.10/site-packages/django/contrib/sessions/backends/base.py", line 94, in encode
return signing.dumps(
File "/home/amw/.local/lib/python3.10/site-packages/django/core/signing.py", line 150, in dumps
return TimestampSigner(key, salt=salt).sign_object(
File "/home/amw/.local/lib/python3.10/site-packages/django/core/signing.py", line 228, in sign_object
data = serializer().dumps(obj)
File "/home/amw/.local/lib/python3.10/site-packages/django/core/signing.py", line 125, in dumps
return json.dumps(obj, separators=(",", ":")).encode("latin-1")
File "/usr/lib/python3.10/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python3.10/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.10/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.10/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
Exception Type: TypeError at /orders/create/
Exception Value: Object of type Decimal is not JSON serializable
the View
def OrderCreateView(request):
bag = Bag(request)
if request.method == 'POST':
order_create_form = OrderCreateForm(request.POST)
if order_create_form.is_valid():
order = order_create_form.save()
for item in bag:
OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity'])
bag.clear() # -- Clear The Bag --
context = {'order': order, 'page': 'Checkout'}
return render(request, 'Orders/Order/Created.html', context)
else:
order_create_form = OrderCreateForm()
context = {'bag': bag, 'order_create_form': order_create_form, 'page': 'Checkout'}
return render(request, 'Orders/Order/Create.html', context)
model
class Order(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField()
address = models.CharField(max_length=250)
postal_code = models.CharField(max_length=20)
city = models.CharField(max_length=100)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
paid = models.BooleanField(default=False)
class Meta:
ordering = ('-created',)
def __str__(self):
return f'Order {self.id}'
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='items', on_delete=models.CASCADE)
product = models.ForeignKey(Product, related_name='order_items', on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
quantity = models.PositiveIntegerField(default=1)
def __str__(self):
return str(self.id)
def get_cost(self):
return self.price * self.quantity