1
我在django
建立一個店亭,我有一個視圖,顯示一些時間範圍內銷售的產品如此;django意見與django tastypie
def product_sold_report(request): response = {} id_list = [] try: _start = _get_parameter(request, "_start") except Exception, e: _start = None try: _end = _get_parameter(request, "_end") except Exception, e: _end = None if _start and _end: orders = Order.objects.filter(created__range=[datetime.datetime.fromtimestamp(float(_start)),datetime.datetime.fromtimestamp(float(_end))]).filter(status = 4).order_by("-created") else: orders = Order.objects.all().filter(status=4).order_by("-created") for order in orders: id_list.append(order.id) for item in OrderItem.objects.filter(order__in = id_list): i = Order.objects.get(id = item.order_id) try: product = Product.objects.get(id = item.product_reference) barcode = product.barcode except Exception,e: barcode = None if item.product_name in response: response[item.product_name]["product_quantity"] += item.quantity else: response[item.product_name] = { "product_quantity":item.quantity, "product_barcode":barcode } return HttpResponse(simplejson.dumps(response), mimetype="text/json")
不過,我想用一個REST框架,這樣我可以有另一臺服務器上查詢這個應用程序不同的Web應用程序,並得到同樣的產品賣出的結果,我認爲django tastypie
,但它似乎主要很多在模型資源上。是否有可能使用django tastypie
或django rest framework
完成此操作。
謝謝
感謝很多黑客人生的答案,我想我會這樣做,因爲我真的找不到方法讓django tastypie返回我想要的數據。順便說一下,你認爲編寫自定義視圖會有什麼性能問題?謝謝 –
如果您確定查詢效率很高,請不要使用。確保在處理數據時使用你需要的一次性訪問數據,然後查看諸如解析返回的數據字典等內容。你打的服務器越少,其他信息越好 –