Attribute error

39 views
Skip to first unread message

Peter Kirieny

unread,
May 12, 2021, 4:52:02 AM5/12/21
to django...@googlegroups.com
can somebody help with this please, am building an ecommerce web and when i want to view my cart i get this error
AttributeError: 'NoneType' object has no attribute 'price'

here is my models.py
class OrderItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
order = models.ForeignKey(Order, on_delete=models.SET_NULL, null=True)
quantity = models.IntegerField(default=0, null=True, blank=True)
date_added = models.DateTimeField(auto_now_add=True)

@property
def get_total(self):
total = self.product.price * self.quantity
return total


AttributeError at /cart/

'NoneType' object has no attribute 'price'
Request Method:GET
Request URL:http://127.0.0.1:8000/cart/
Django Version:3.2
Exception Type:AttributeError
Exception Value:
'NoneType' object has no attribute 'price'
Exception Location:C:\Users\Admin\PycharmProject\MyProject\Ecommerce\shop\models.py, line 71, in get_total
Python Executable:C:\Users\Admin\PycharmProject\MyProject\venv\Scripts\python.exe
Python Version:3.8.2
Python Path:
['C:\\Users\\Admin\\PycharmProject\\MyProject\\Ecommerce',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\python38.zip',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\DLLs',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38\\lib',
 'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python38',
 'C:\\Users\\Admin\\PycharmProject\\MyProject\\venv',
 'C:\\Users\\Admin\\PycharmProject\\MyProject\\venv\\lib\\site-packages']
Server time:Wed, 12 May 2021 08:44:09 +0000


Ryan Nowakowski

unread,
May 13, 2021, 8:32:12 AM5/13/21
to django...@googlegroups.com
It's likely that your instance of OrderItem has a product field that's null.

Peter Kirieny

unread,
May 18, 2021, 7:32:19 PM5/18/21
to django...@googlegroups.com
tanks for this, it was helpfull


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/FDF6B1C2-9134-43D2-8057-EA3BCF14C2A1%40fattuba.com.
Reply all
Reply to author
Forward
0 new messages