Its simple... in your fee models' save method check that
self.remaining_fees is zero then make self.completed field to true..
def save(self, *args, **kwargs):
self.remaining_fees = self.school_fees - self.paid_fees
if self.remaining_fees == 0:
self.completed = True
super().save(*args, **kwargs)
On Monday, September 14, 2020 at 7:11:51 AM UTC-4 [email protected] wrote:
> Thank you brother @coolguy, I have put to work your latest solution and
> it has worked like a charm.I have been learning a lot from you lately, as
> you know i am still a beginner, i would love to learn one more thing from
> you..i have a boolean field "completed", and i want django to automatically
> check it when balance_fees or remaining_fees is equal to zero,
>
> Here is my model
> [image: image.png]
>
> my template
> [image: image.png]
>
>
>
>
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
>
> *From: *coolguy
> *Sent: *Friday, September 11, 2020 8:51 PM
> *To: *Django users
> *Subject: *Re: Help me with the view please
>
>
>
> Technically your Fee model is designed incorrectly. You should have
> separate models for 'fees' and 'fees_paid' where fee would have been the
> foreign key in 'fees_paid. Fee should have a property like annual fee etc
> for each student as fee may be different based on student discounts etc.
> Then when student pays the fees, fees should go in a separate transactional
> table i.e. 'fees paid'. In your scenario, does each row in fee table
> carries the annual/school fee? if so then yes summing them up will
> definitely cause incorrect figure.
>
>
>
> Here is what i would do if i don't change my models structure.
>
>
>
> views.py
>
> =======
>
> def student_detail(request, pk):
>
> student = Student.objects.get(id=pk)
>
>
>
> *# in your case pick the first record from fee_set*
>
> first_record = student.fee_set.first()
>
> school_fees = first_record.school_fees
>
>
>
> paid_fees = student.fee_set.aggregate(total_paid_fees=Sum('paid_fees'))
>
>
>
> balance_fees = school_fees - paid_fees["total_paid_fees"]
>
>
>
> context = {"student": student,
>
> "paid_fees": paid_fees,
>
> "school_fees": school_fees,
>
> "balance_fees": balance_fees, }
>
>
>
> return render(request, "student_details.html", context)
>
>
>
> template
>
> =======
>
> <body>
>
> <h1>Student Detail</h1>
>
> <p>Student name: {{ student.name }}</p>
>
> <p>School fees: {{ school_fees }}</p>
>
> <p>Paid fees: {{ paid_fees.total_paid_fees }}</p>
>
> <p>Remaining fees: {{ balance_fees }}</p>
>
> </body>
>
>
>
> Output:
>
> ================
>
> Student name: student1
>
> School fees: 100000.0
>
> Paid fees: 35000.0
>
> Remaining fees: 65000.0
>
>
>
> On Friday, September 11, 2020 at 5:25:28 AM UTC-4 [email protected] wrote:
>
> Sorry for bothering you @coolguy, you’re the only person who seems to be
> nice to a Django beginner developer like me, and I would like to bother you
> one more time
>
> Your solution for using methods in models is perfectly fine
>
> You see I want school_fees to be a fixed amount, example $300
>
> So when a student pays more than once(student.fee_set), lets say this
> month student 1 pays $50
>
> And another month pays $100 dollars, I want payments to added and
> subtracted from fixed schools_fees($300)
>
> Remainingfees to be $300 – (($50)+($100))
>
>
>
> BUT what “
> fees = student.fee_set.aggregate(total_paid_fees=Sum('paid_fees'),
> total_school_fees=Sum('school_fees'))”
> does is that it sums both paid fees school fees every time a student pays,
> I want only paid fees to added every time a student pays but not school fees
>
>
>
>
>
>
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
>
> *From: *coolguy
> *Sent: *Friday, September 11, 2020 4:27 AM
> *To: *Django users
> *Subject: *Re: Help me with the view please
>
>
>
> If i choose to go with your way of calculation, i would do this...
>
>
>
> def student_detail(request, pk):
>
> student = Student.objects.get(id=pk)
>
>
> fees = student.fee_set.aggregate(total_paid_fees=Sum('paid_fees'),
> total_school_fees=Sum('school_fees'))
>
>
>
> balance_fees = fees["total_school_fees"] - fees["total_paid_fees"]
>
>
>
> context = {"student": student,
> "fees": fees, "balance_fees": balance_fees, }
>
>
>
> return render(request, "student_details.html", context)
>
>
>
> Template e.g.
>
> <body>
>
> <h1>Student Detail</h1>
>
> <p>Student name: {{ student.name }}</p>
>
> <p>School fees: {{ fees.total_school_fees }}</p>
>
> <p>Paid fees: {{ fees.total_paid_fees }}</p>
>
> <p>Remaining fees: {{ balance_fees }}</p>
>
> </body>
>
>
>
> Result:
>
> Student Detail
>
> Student name: student1
>
> School fees: 100000.0
>
> Paid fees: 25000.0
>
> Remaining fees: 75000.0
>
> I hope this helps!
>
> On Thursday, September 10, 2020 at 2:16:27 PM UTC-4 [email protected]
> wrote:
>
> Thanks for helping me @coolguy,but it seems I ran into another problem,
> the school fees and remaining fees somehow sums together, I want students
> to pay school fees in phases, i.e, first quarter, second etc, and I want
> remaining fee to be the difference between the initial school fees which is
> 1000000 and total fee paid.
>
> Thanks in advance
>
>
>
> My views.py for student
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
>
> *From: *coolguy
> *Sent: *Tuesday, September 8, 2020 11:58 PM
> *To: *Django users
> *Subject: *Re: Help me with the view please
>
>
>
> I see now what you are asking...
>
>
>
> I never do such calculations in views rather I create methods in models.py
> to do so and call them in the template...
>
>
>
> Like what you could do is..
>
>
>
> class Student(models.Model):
>
> name = models.CharField(max_length=200, null=True, blank=True)
>
> classroom = models.ForeignKey(Classroom,
>
>
> on_delete=models.DO_NOTHING, blank=True,
> null=True)
>
>
>
> def __str__(self):
>
> return *self*.name
>
>
>
> def get_total_fee(self):
>
> return sum(student.school_fees for student in *self*
> .fee_set.all())
>
>
>
> def get_total_paid_fee(self):
>
> return sum(student.paid_fees for student in *self*.fee_set.all())
>
>
>
> def get_remaining_fee(self):
>
> total_fee = *self*.get_total_fee()
>
> total_paid = *self*.get_total_paid_fee()
>
> return float(total_fee - total_paid)
>
>
>
> and in template you can call...
>
> {{ student.get_remaining_fee }}
>
> On Tuesday, September 8, 2020 at 1:35:56 PM UTC-4 [email protected] wrote:
>
> Thank you for your help @coolguy..but my real problem lies in writing the
> code for “fee_remaining”, can you help me with that also..thanks
>
>
>
> Sent from Mail <https://go.microsoft.com/fwlink/?LinkId=550986> for
> Windows 10
>
>
>
> *From: *coolguy
> *Sent: *Tuesday, September 8, 2020 7:45 PM
> *To: *Django users
> *Subject: *Re: Help me with the view please
>
>
>
> In your return line of code, you are referring to context variable but I
> don't see you assigned any value to this context variable in your code.
>
> Or if you want to send three variables i.e. "fees", "total_fees" and
> "fee_remaining"... you need to send them separately like
>
>
>
> return render(request, 'website/students.html', {"fees": fees,
> "total_fees": total_fees, "fee_remaining": fee_remaining })
>
>
>
> then use these variables on your students.html template.
>
>
>
> On Tuesday, September 8, 2020 at 12:21:21 PM UTC-4 [email protected]
> wrote:
>
> My Models
> class Student(models.Model):
> name = models.CharField(max_length=200, null=True, blank=True)
> classroom = models.ForeignKey(‘Classroom’,
> on_delete=models.DO_NOTHING,blank=True, null=True)
> class Classroom(models.Model):
> name = models.CharField(max_length=40,blank=True, null=True)
>
> class Fee(models.Model):
> student = models.ForeignKey(Student, on_delete=models.CASCADE, null=True,)
> classroom = models.ForeignKey(Classroom, on_delete=models.CASCADE,
> null=True)
> school_fees = models.FloatField(default=1000000)
> paid_fees = models.FloatField(null=False)
> remaining_fees = models.FloatField(blank=True)
> completed = models.BooleanField(null=False, default=False)
>
> views.py
> def student(request, pk):
> student = Student.objects.get(id=pk)
>
> fees = student.fee_set.all().order_by('-publish_date') total_fees =
> student.fee_set.all().filter(student__id=pk)
> .aggregate(sum=Sum('paid_fees', flat=True)['sum']
>
> fees_remaining = ()
>
> return render(request, 'website/students.html', context)
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e026b778-bac8-4522-ad1f-2ea4b33ad1efn%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/e026b778-bac8-4522-ad1f-2ea4b33ad1efn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/bc5f52f8-4b46-40c5-8ff1-9d05e3095346n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/bc5f52f8-4b46-40c5-8ff1-9d05e3095346n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/aca1c587-c6e9-4bf6-8c07-0d2927795e82n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/aca1c587-c6e9-4bf6-8c07-0d2927795e82n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
> --
> 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 [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4f854ccd-3bd0-4b8c-b17c-03e32e8f56dbn%40googlegroups.com
>
> <https://groups.google.com/d/msgid/django-users/4f854ccd-3bd0-4b8c-b17c-03e32e8f56dbn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/7f79d2ce-17e7-43f0-9c8f-c5b3afe6b21fn%40googlegroups.com.