Tuesday, March 28, 2023

Using one query set for another query

Hello all,

    I am trying to use the query of:
paid_bills = bill_payment_history.objects.filter(date_paid__year='2023', date_paid__month='03')

as criteria for another query to only show the unpaid bills.

The known_bills model is my control list of known bills that occur frequently.
The  bill_payment_history model is my bill transaction table.  It is a running history of transactions for bill payment.

A sample of known_bills records is:
bill_1
bill_2
bill_3

A sample of  bill_payment_history is:
bill_1 paid last month
bill_2 paid last month
bill_3 paid last month
bill_1 paid this month
bill_2 paid this month
bill_3 paid this month

What I am trying to do is use bill_payment_history filtered on date_paid by year and month and query the known_bills table to see what bills have not been paid yet.  I keep trying something similar to:  results = paid_bills.exclude(short_description__in=known_bills)

but my results either keep coming up with all of the records or none of them.  What am I doing wrong?

Here is the model information:

class known_bills(models.Model):
    #Full bank transaction
    description = models.CharField(max_length=255)
    #String value to search the CSV file with.
    short_description = models.CharField(max_length=255, unique=True)
    #Value I know it as.
    friendly_name = models.CharField(max_length=255)
    expected_due_date = models.DateField()
    expected_cost = models.DecimalField(max_digits=6,decimal_places=2)

class bill_payment_history(models.Model):
    description = models.CharField(max_length=255)
    short_description = models.ForeignKey(known_bills, on_delete=models.CASCADE)
    friendly_name = models.CharField(max_length=255)
    date_paid = models.DateField()
    cost = models.DecimalField(max_digits=6,decimal_places=2)

    class Meta:
        unique_together = (
            ('short_description',
            'date_paid'),
            )

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6a9327f8-6a00-49d2-8411-bd9fb03c64c0n%40googlegroups.com.

No comments:

Post a Comment