[Django]-AttributeError in views.py: type object 'Transaction' has no attribute 'objects'

4👍

You don’t need a second Transaction class in your views. It is shadowing the Transaction model/class imported from your model. Remove it!

More so, you also don’t need a custom sum function, use the builtin sum:

def index(request):
    transactions = Transaction.objects.all()
    balance = sum(t.amount if t.name=='deposit' else -t.amount for t in transactions)
    ...

1👍

The Transaction class that you have in your views.py file shadows your Transaction model.

Transaction in views.py file is a simple python class (not a Django model), to fix your issue, you need to remove it from views.py

Leave a comment