Home > Blockchain >  Django - Count every installment in a balance every month
Django - Count every installment in a balance every month

Time:01-20

I made a program to control bills and monthly installments. I don't know if the way I made my Model is wrong, but I achieved to make the month we are now months to pay.

Question: The for that I'm using isn't running the whole range (0, month), it's returning the month and showing, is there a way to make the for in the template to run the whole range or something different ?

Here's a picture of 2 month installment, I want to appear in 2 months, but only appear in the last (Feb) enter image description here

Now I want in the view to show in balance the installments and its months (i.e: Jan and Feb), but I'm stuck. Can anyone help me, please ?

Model.py

class Balance(models.Model):

account = models.ForeignKey(Account, null=True, on_delete=models.SET_NULL)
value = models.FloatField(null=True)
installment = models.IntegerField(null=True, default=0) #How many installments
installment_value = models.FloatField(null=True, default=0) #Value of it
months = models.IntegerField(null=True, default=0) #From a range (1, 12) calculates the last month

def save(self, *args, **kwargs):
    self.months = (int(datetime.now().strftime("%m"))   int(self.installment))-1
    if self.months > 12:
        self.months = int(self.months) - 12
    super().save(*args, **kwargs)

View.py

#BALANCE
def balance(request, pk):
    name = Client.objects.all().get(user=request.user)
    account = Account.objects.all().get(id=pk)
    balance = account.balance_set.all()

ctx = {'account': account, 'name': name, 'balance': balance,
       'months': {'Jan': 1, 'Feb': 2, 'Mar': 3,
                 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7,
                 'Aug': 8, 'Sep': 9, 'Oct': 10,
                 'Nov': 11, 'Dec': 12}} #Months dict is for view, it goes in a for loop to appear in the same page

return render(request, 'balance.html', ctx)

Form

def pay(request, pk):
    name = Client.objects.all().get(user=request.user)
    account = Account.objects.all().get(id=pk)
    balance = account.balance_set.all()

    if request.method == "POST":
        form_install = Installment(request.POST)
        form_balance = Balance(request.POST)
        if form_install.is_valid() and form_balance.is_valid():
            bal = form_balance
            total_value = ext['value'].value()
            Account.objects.filter(id=pk).update(credit=F('credit') - total_value)
            bal.save()
            return redirect(f'/credito/{pk}')

Template

<div >
            {% for key, value in months.items %}
            <table >
                <tr>
                    <th>Value</th>
                    <th>Installments</th>
                    <th>Installments Value</th>
                </tr>
                <h4>{{key|upper}}</h4>
                <tr>
                    {% for bal in balance %}
                    {% if value == bal.months %}
                        <td>${{bal.value}}</td>
                        <td>{{bal.installment}}x</td>
                        <td>${{bal.installment_value}}</td>
                </tr>
                {% endif %}
                {% endfor %}
                {% endfor %}
            </table>
        </div>

Thank you !

CodePudding user response:

I want to thank @Henty for the help !

As my problem, I solved it using a list, the for lookup will run with the biggest number in a list in parallel.

So we have an installment in 8 months and another in 2 months. I create a list

list = [] #This list receives the number (i.e: 2 and 8)
    for i in balance:
        for j in range(i.months): #This for will run through the range
            j  = 1 #Just to make the index 1
            list.append(j) #APPEND [1, 2, 1, 2, 3, 4, 5, 6, 7, 8]
    final_list = list(dict.fromkeys(list))
    print(final_list) # OUTPUT: [1, 2, 3, 4, 5, 6, 7, 8]

And in the template:

{% for l in list %} #RUN THE LIST
{% for bal in balance %} #RUN THE MODEL
{% if l == value and bal.months >= l %} #THIS MAKES IT APPEARS IN EVERY MONTH FROM 1 TO FINAL MONTH

It's working so far, thank you all !

  •  Tags:  
  • Related