Exception Value: Field ‘id’ expected a number but got

I am having problems with passing the Post request via Vue.js in Django. It keeps getting this error when I try to fill up the form and click buy. What I am doing is that I pass it through an api and try to send it to the database and save it there. The page continues working fine, but on the background nothing happens. In console it says error 500 and when clicking on Network it shows all of that information. I’ve been following this tutorial while building my own stuff: https://www.youtube.com/watch?v=2Ed3fTYcx-w&list=PLpyspNLjzwBmIDrDOaPkLLuy5YDDNW9SA&index=8&ab_channel=CodeWithStein

I have fixed that problem and now it gives me that: JSONDecodeError at /api/checkout/, Expecting value: line 1 column 1 (char 0)

        var productapp = new Vue({
            el:'#cartapp',
            delimiters:['[[', ']]'],
            store: store,
            data() {
                return {
                    first_name: '',
                    last_name: '',
                    birth_date: '',
                    phone: '',
                    email: '',
                    city: '',
                    region: '',
                    address: '',
                    references: '',

                    products: [{{productsstring|safe}}]
                }
            },
            computed: {
                numItems: function() {
                    return store.state.numItems
                },
                totalCost: function() {
                    return store.state.totalCost
                }
            },
            methods: {
                submitForm() {

                    var data = {
                        'first_name': this.first_name,
                        'last_name': this.last_name,
                        'birth_date': this.birth_date,
                        'phone': this.phone,
                        'email': this.email,
                        'city': this.city,
                        'region': this.region,
                        'address': this.address,
                        'references': this.references,
                    };

                    fetch('/api/checkout/', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-CSRFToken': '{{ csrf_token }}'
                        },
                        credentials: 'same-origin',
                        body: JSON.stringify(data),
                    })
                    .then((response) => {
                        console.log('success')
                        console.log(response)
                    })
                    .catch(function (error) {
                        console.log('Error 2');
                    })
API.PY FILE:

import json
from django.http import JsonResponse
from cart.models import Cart
from .models import Product
from order.models import Order, OrderItem
from order.utils import checkout
from django.shortcuts import get_object_or_404, redirect

def api_checkout(request):
    cart = Cart(request)
    data = json.loads(request.body)
    jsonresponse = {'success': True}

    first_name = data['first_name']
    last_name = data['last_name']
    birth_date = data['birth_date']
    phone = data['phone']
    email = data['email']
    city = data['city']
    region = data['region']
    address = data['address']
    references = data['references']

    orderid = checkout(request, first_name, last_name, birth_date, phone , email, city, region, address, references)
    paid = True

    if paid == True:
        order = Order.objects(pk=orderid)
        order.paid = True
        order.paid_amount = cart.get_total_cost()
        order.save()

        cart.clear()
    
    return JsonResponse(jsonresponse)
    
    
UTILS.PY FILE:


import datetime
import os
from random import randint
from cart.models import Cart
from .models import Order, OrderItem

def checkout(request, first_name, last_name, birth_date, phone, email, city, region, address, references):
    order = Order(first_name=first_name,  last_name=last_name, birth_date=birth_date, phone=phone, email=email, city=city, region=region, address=address, references=references)
    order.save()
    cart = Cart(request)
    for item in cart:
        OrderItem.objects.create(order=order, product=item['product'], price=item['price'], quantity=item['quantity'])
        return order.id
Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/api/checkout/

Django Version: 3.1.7
Python Version: 3.9.0
Installed Applications:
['order.apps.OrderConfig',
 'cart.apps.CartConfig',
 'crispy_forms',
 'ckeditor',
 'shop.apps.ShopConfig',
 'categories.apps.CategoriesConfig',
 'pages.apps.PagesConfig',
 'blog.apps.BlogConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

enter image description here

Answer

Probably it happens here: order = Order(request... Why are you passing the request object to the Order model? It’s not expecting such argument. Just remove it when creating a new Order in the checkout function and it’ll be fixed.

UPD1: please print what’s in the request.body before trying to parse it. I think something is missing there.