我有一个QuerySet,我需要将它发送到我的模板,因为我需要操纵它的值来生成一个JS数组。
但在我看来,我使用:
order_items = json.dumps(order_items)
我明白了:
Object of type QuerySet is not JSON serializable
视图:
import json
def thanks_deposit_payment(request):
order_number = Order.objects.latest('id').id
total = Order.objects.latest('id').total
costo_despacho = Order.objects.latest('id').shipping_cost
order_items = OrderItem.objects.filter(order=Order.objects.latest('id'))
# order_items = json.dumps(order_items)
response = render(request, 'thanks_deposit_payment.html', dict(order_number=order_number, total=total,
order_items=order_items, costo_despacho=costo_despacho))
return response
models.py:
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.CharField(max_length= 200)
quantity = models.CharField(max_length= 200)
size = models.CharField(max_length=200)
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name= 'PEN Price')
file = models.FileField(upload_to='files', blank=True, null=True)
comment = models.CharField(max_length=200, blank=True, null=True, default='')
uploaded_at = models.DateTimeField(auto_now_add=True)
class Meta:
db_table = "OrderItem"
我需要使用从后端发送的order_items中的项来生成products数组:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: 'eec.purchase',
ecommerce: {
currencyCode: 'PEN',
purchase: {
actionField: {
id: {{ order_number }},
affiliation: 'Stickers Gallito E-Commerce',
revenue: {{ total }},
shipping: {{ costo_despacho }},
coupon: 'SUMMER2019'
}
},
products: [
拉风的咖菲猫
墨色风雨
相关分类