2017-02-27 4 views
0

사용자가있는 경우 저장하고 계산하는 staticmethod가 있습니다.django 데이터베이스에서 필드 값을 가져 오는 중

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') 

다음과 내가보기에 얻는 계산 될 때 : 다음과 같이 나는 결과를 얻으려면

@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} 

그리고 전망이다

{'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에 할당된다

. calculation['calculation']__getitem__ 메서드가없는 Calculation 개체이므로 dict처럼 사용할 수 없습니다.

대신해야

price = calculation['calculation'].purchase_price 
관련 문제