Saturday, June 30, 2018

Re: Queryset calculation and then filter/exclude/limit data rendered

I figured it out.  Probably not the ideal solution, but it works for now.

I did not realize you could use an IF tag in the the template.  I then passed the fromDate and toDate into the template and used that to limit the records to only that particular month.

Hopefully as my skills increase I will find more elegant solutions.

rod

On Friday, June 29, 2018 at 3:02:58 PM UTC-5, rod termaat wrote:
I totally get what you are saying and thanks.  I tried a similar strategy.  I do qs = Check.objects.all()  and then I have to do the calculation as there is no balance in the model so I generate it on the fly in the for loop.  My hope was to then filter qs like you show.  It works, but my balance calculation is lost which is integral to the process.

my filter code looks like:
monthdata = check_list.filter(dater__gte=CalendarFromMonth, dater__lte=CalendarToMonth)

After some research that seemed like expected behavior where filter goes back thru the OCR where as I was hoping it would just filter on check_list and therefore retain the balance amount.

Thanks for your perspective, rod


On Friday, June 29, 2018 at 3:13:46 AM UTC-5, Derek wrote:
Its not completely clear what you are doing.

You need to avoid logic in your template; but you say "excluding data from the the queryset in the for loop which is easy in other languages, but this also seemed to be a no go".  That is not the case if your logic is written in the correct way.

In my case I need to present a month-by-month view of orders; the UI displays day-by-day entries for the selected month (defaults to current month) with two "buttons" (hyperlinks) that are set to a +1 and -1 delta relative to the current month number so the user can jump back and forth easily.  The function in the views.py file accepts the month number as an argument and filters orders based on that month e.g. in your case you might do:

qs = Check.objects.all()
if selected_month:      qs = qs.filter(created__month=int(selected_month))
where selected_month would be a argument passed into your view.  You can then apply other calculation logic on the list.  


On Thursday, 28 June 2018 02:09:21 UTC+2, rod termaat wrote:

I have a small checkbook application where I store transactions (expenses and income) amounts by date.  I render these to a template using python to add the cumulative balance prior to render.  This work fine for my little application, but not too sure how scalable it will be over time.


My goal is to have this displayed by month with the running balance.  I first researched custom pagination, but found nothing on date pagination by month and year.  I then started down the road of excluding data from the the queryset in the for loop which is easy in other languages, but this also seemed to be a no go.  My current thought is to exclude the data from the queryset in the for loop and move the data into a dictionary or list, or json.


Any advice for me?  Right now I need all objects from the Check model to get an accurate balance and then I need to filter that to limit the records by month.


Finally, I did tried to filter the check_list after the balance was added, but found out that does not work as the query goes back to the model and I loose the balance.


Here is my model


class Check(models.Model):

    """

    Model representing a checkbook transaction.

    """

    check_type = (('CR', 'credit'),('DR', 'debit'),('SV', 'savings'),('DT', 'debt reduction'),)

    

    dater = models.DateField(help_text=' .the date of the transaction')

    type = models.CharField(max_length=2, choices=check_type, help_text=' .credit or debit')

    category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)

    name = models.CharField(max_length=100, help_text=' .description')

    amount = models.IntegerField(default=0, help_text=' .amount')

    cleared = models.BooleanField(help_text=' .cleared with the bank?')

    created = models.DateTimeField(auto_now_add=True)


Here is my function view


def checkbookList(request):

    check_list = Check.objects.all()

    #checks = Check.objects.all()

   

    balance = 0

    #for chk in checks:

    for chk in check_list:

        balance += chk.amount

        chk.balance = balance


        if chk.cleared:

            chk.cleared = '\u221A'

        else:

            chk.cleared = '-'


    return render(request, 'checkbook/checkbook.html', {'checks': check_list})


--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/25ff02ac-5b44-4cde-94e7-142167214285%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment