-1

I have added a custom method to my fee profile model, and i wanted it to calculate for me the fee balance when a fee instance is created. First, before adding the custom method, everything was working out well. I could add a fee profile instance in the django admin without getting errors. Also, i have included a dictionary that stores the fee payment history and am unable to figure out a way to display the fee payment history on a template.I am running django 3.1.4, and using python 3.8.5. Here is my code:

This is the error i got.

"str returned non-string (type Child)

All was working well till i introduced the fee_status() custom method.

admin.py

@admin.register(FeeProfile)
class FeeProfile(admin.ModelAdmin):
    list_display = (
        'child', 'amount_paid', 'date', 'fee_status'
    )
    list_filter = ['date']
    search_fields = ['child']

models.py

class FeeProfile(models.Model):
    child = models.ForeignKey('bjs_main.Child', on_delete=models.CASCADE)
    parent = models.ForeignKey('bjs_main.Parent', on_delete=models.CASCADE)
    mpesa_code = models.CharField(max_length=10, primary_key=True)
    amount_paid = models.IntegerField(default=0)
    date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.child

    def fee_status(self):
        global total_amount_paid
        total_amount_paid += self.amount_paid
        fee_balance = term_fee - total_amount_paid
        payment_history = {
            'amount_paid': self.amount_paid,
            'date_paid': self.date
           }
        return fee_balance

views.py

class FeeListView(LoginRequiredMixin, generic.ListView):
    model = FeeProfile
    context_object_name = 'fee_profile'
    template_name = 'fees/feeprofile_list.html'
    queryset = FeeProfile.objects.all()[:]'''
James Z
  • 12,209
  • 10
  • 24
  • 44
Shuji
  • 1
  • 1

1 Answers1

-1

the error seems to be from the str function, you are outputting an instance of a child object therefor it is throwing the error "str returned non-string (type Child)", it expects an instance of string can you try:

def __str__(self):
    return str(self.child)

As for the calculated field (fee_status) i would use this answer How to add a calculated field to a Django model

Sirwill98
  • 324
  • 1
  • 13