[Answered ]-Trying to use a Model method in View Django

1👍

getTransaction is in your method is a QuerySet and thus a collection of items, not a single item, hence getTransaction.get_quantities_sold makes no sense. You can retrieve a single Transaction object by using .get(…) [Django-doc] over .filter(…) [Django-doc]:

getTransaction = Transaction.objects.get(
    reference_num=serializer.data['reference_num']
)

It might also be worth to guarantee uniqueness for the reference_num with:

reference_num = models.CharField(
    max_length=50,
    editable=False,
    default=generate_ref_no,
    unique=True
)

Leave a comment