B
B
blazer052017-07-07 09:08:21
Django
blazer05, 2017-07-07 09:08:21

'Order' object has no attribute 'items' - How to fix error?

The error is raised in the models.py file of the Orders application in this function
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
But if this dictionary line is commented out in the views.py file of the payment application
'amount': '%.2f' % order.get_total_cost().quantize(Decimal('.01')),
then there is no error, but it shouldn't be.
model
https://github.com/dikiigr/django-online-shop/blob...
and view
https://github.com/dikiigr/django-online-shop/blob...

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/payment/process/

Django Version: 1.11.2
Python Version: 2.7.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'shop',
 'cart',
 'orders',
 'paypal.standard.ipn',
 'payment']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "D:\OpenServer\domains\virtualenv\myshop\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "D:\OpenServer\domains\virtualenv\myshop\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "D:\OpenServer\domains\virtualenv\myshop\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\OpenServer\domains\myshop\payment\views.py" in PaymentProcess
  19.         'amount': '%.2f' % order.get_total_cost().quantize(Decimal('.01')),

File "D:\OpenServer\domains\myshop\orders\models.py" in get_total_cost
  27.         return sum(item.get_cost() for item in self.items.all())

Exception Type: AttributeError at /payment/process/
Exception Value: 'Order' object has no attribute 'items'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aRegius, 2017-07-07
@blazer05

Python sees it
and asks you what is an instance attribute of the class Order items , where did you get it from if it is not defined anywhere in the class?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question