Reputation: 12064
I have an staticmethod where I save user (if exists) and calculation.
@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}
And the view, where I want to get the results is as follows :
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')
The calculation which I get in the view is as follows :
{'statusCode': 200, 'calculation': <Calculations: Calculation for user>, 'customer': None}
How can I now get the purchase_price
from calculation? I tried with
price = calculation['calculation']['purchase_price']
But I get an error : TypeError: 'Calculations' object is not subscriptable
Any advice?
Upvotes: 0
Views: 38
Reputation: 2062
You are returning
{'statusCode': 200, 'calculation': <Calculations: Calculation for user>, 'customer': None}
and assigning it to calculation
.
Your calculation['calculation']
is Calculation
object which does not have __getitem__
method, so u can't use it like dict
.
You should instead do
price = calculation['calculation'].purchase_price
Upvotes: 1