2017-02-27 8 views
0

私はユーザが存在する場合にはその静的メソッドと計算を保存しています。djangoデータベースからフィールド値を取得する

@staticmethod 
    def save_calculation(user, selection, calculation_data): 
     customer = None 

     if calculation_data['firstname'] or calculation_data['lastname']: 
      customer = Customer() 
      customer.title = calculation_data['title'] 
      customer.firstname = calculation_data['firstname'] 
      customer.lastname = calculation_data['lastname'] 
      customer.save() 


     n_calculation = Calculations() 
     n_calculation.user = user 
     n_calculation.category = selection['category_name'] 
     n_calculation.make = selection['make_name'] 
     n_calculation.model = selection['model_name'] 
     n_calculation.purchase_price = selection['purchase_price'] 
     n_calculation.customer = customer 
     n_calculation.save() 
     return {'statusCode': 200, 'calculation': n_calculation, 'customer': customer} 

そして、私は結果を取得したいビューは、次のようにされて次のように私はビューに入る計算がある

def adviced_price(request): 

if request.method == 'POST': 
    connector = Adapter(Connector) 
    selection = Selection(request).to_dict() 
    calculation = connector.calculations(request.user, selection, request.POST) 

    if 'statusCode' in calculation and calculation['statusCode'] == 200: 
     customer = '' 
     if 'customer' in calculation: 
      customer = calculation['customer'] 

     price = calculation['calculation']['purchase_price'] # How to get the price 
     context = {"calculation_data": calculation['calculation'], 'customer': customer, 'price': price} 
     return render(request, 'master/result-calculation.html', context) 
    else: 
     return 
else: 
    return HttpResponse('Not POST') 

{'statusCode': 200, 'calculation': <Calculations: Calculation for user>, 'customer': None} 

どのようにすることができます計算の結果はpurchase_priceになりますか?私は

price = calculation['calculation']['purchase_price'] 

と試みたが、私はエラーを取得:TypeError: 'Calculations' object is not subscriptable

何かアドバイスを?

答えて

1

あなたは

{'statusCode': 200, 'calculation': <Calculations: Calculation for user>, 'customer': None}

calculationに割り当てるに戻ってきています。 は__getitem__メソッドを持たないCalculationオブジェクトですので、dictのように使用することはできません。

代わりに行う必要があります

price = calculation['calculation'].purchase_price 
関連する問題