I have a model that contains a many to many field:
class Transaction(models.Model):
id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False, primary_key=True)
transaction_date = models.DateField(null=False, blank=False)
transaction_category = models.ForeignKey(Category, on_delete=models.PROTECT, null=False, blank=False)
customer_vendor = models.ForeignKey(CustomerVendor, on_delete=models.PROTECT, blank=False, null=False)
account = models.ForeignKey(Account, on_delete=models.PROTECT, blank=False, null=False)
reimbursable = models.BooleanField()
tag = models.ManyToManyField(Tag, blank=True)
amount = models.DecimalField(max_digits=12, decimal_places=2, null=False, blank=False)
description = models.CharField(max_length=255, null=False, blank=False)
I have a query that gets me transactions:
Transaction.objects.filter(transaction_category__master_category__category_type="Income", transaction_date__range=[start_date, end_date]).filter(exclude_reimbursable).order_by().annotate(month=ExtractMonth("transaction_date"), year=ExtractYear("transaction_date")).values("month", "year").annotate(month_year=Concat("month", V(" "), "year", output_field=CharField()), month_total=Sum("amount")).values("month_year", "month_total").order_by("year", "month")
I am trying to add the Tag (many to many field) to the query. From the web app, I get all the tags that a user selected as a list. The list could also be empty and have 0 tags.
I tried using tag__in=tags
in the query, but if a user does not select any tags, it does not work. If a user does not select any tags, we should treat it like any transaction can be returned even if it does not have any tags selected.
I am lost at how to query a many to many field with a empty list.