При обращении к этому файлу запросов, запрашивающем данные из базы данных, я не использую сериализатор, но, как вы видите, используете курсор в необработанном запросе к менеджеру.
view.py
@login_required
def index(request):
if not request.user.is_superuser:
raise(404)
customers = Customer.objects.Customer_List()
context = {'customers': json.dumps(customers)}
return JsonResponse(context=context, safe=False)
model.py
class CustomerListManager(models.Manager):
def Customer_List(self):
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("""
select
customer_id
,last_30_days-perivous_30_days pace
,first_name
,last_name
,company
,tags
,date_created
,latest_order_date
,first_order_date
,customer_group
,store_type
,assigned_to
,total_orders
,days_active
,ofa
,ofaa
,total_spent
,average_order_value
,status
from (
select
customer_id
,last_60_days-last_30_days perivous_30_days
,last_30_days
,first_name
,last_name
,company
,tags
,date_created
,latest_order_date
,first_order_date
,customer_group
,store_type
,assigned_to
,total_orders
,days_active
,ofa
,ofaa
,total_spent
,average_order_value
,status
from
(select
c.customer_id
,c.first_name
,c.last_name
,c.company
,c.tags
,c.date_created
,max(s.date_created) latest_order_date
,min(s.date_created) first_order_date
,g.name as customer_group
,c.store_type
,a.username assigned_to
,(select count(s1.sale_id)
from sales_sale s1
where s1.customer_id =c.customer_id) total_orders
--,max(s.date_created) - min(s.date_created) days_active
,( select max(s.date_created) - min(s.date_created)
from sales_sale s
where c.customer_id=s.customer_id ) days_active
,(select (max(s.date_created) - min(s.date_created))/count(s.sale_id) OFA
from sales_sale s
where s.customer_id =c.customer_id) ofa
,(select (current_date - max(s.date_created))- ( (max(s.date_created) - min(s.date_created))/count(s.sale_id) ) OFA_threshold
from sales_sale s
where s.customer_id =c.customer_id) ofaa
,(select sum(s1.total_inc_tax) total_spent
from sales_sale s1
where s1.customer_id =c.customer_id
and status != 'Cancelled' ) total_spent
,(select count(s1.sale_id)
from sales_sale s1
where s1.customer_id = c.customer_id
and s1.date_created > current_date - interval '30' day
) last_30_days
,(select count(s1.sale_id)
from sales_sale s1
where s1.customer_id =c.customer_id
and s1.date_created > current_date - interval '60' day
) last_60_days
,(select avg(s1.total_inc_tax) average_order_value
from sales_sale s1
where s1.customer_id =c.customer_id) average_order_value
,current_date - max(s.date_created) status
from customers_customer c
left outer join customers_customergroup g on c.customer_group_id=g.id
left outer join auth_user a on c.assigned_to_id=a.id
left outer join sales_sale s on c.customer_id=s.customer_id
left outer join sales_saleitem si on s.id=si.sale_id
-- where c.customer_id =5
group by c.customer_id,c.first_name,c.last_name,c.company
,c.store_credit,c.date_created,g.name,a.username
order by c.customer_id
)total
--where total.total_order >= 1
)total2
""")
columns = [col[0] for col in cursor.description]
result = [
dict(zip(columns, row))
for row in cursor.fetchall()
]
print(result)
return result
Ошибка:
Ошибка типа в / customer /
Объект типа 'datetime' не JSON serializable
Я пытаюсь запросить данные из БД с помощью курсора в менеджере (django модель)