Skip to content

Instantly share code, notes, and snippets.

@martsberger
martsberger / risk_subquery.py
Created January 23, 2018 04:46
Azee Risk Subqueries
# You can put the EqualsAny class in a utility file somewhere, It's necessary
# in order to compare the result of ArrayAgg with a column
from django.db.models import Field
@Field.register_lookup
class EqualsAny(Lookup):
"""
Adds the `__any` lookup for generating SQL like:
@ryanpitts
ryanpitts / gist:1304725
Created October 21, 2011 19:34
GROUP BY and Select MAX from each group in Django, 2 queries
'''
given a Model with:
category = models.CharField(max_length=32, choices=CATEGORY_CHOICES)
pubdate = models.DateTimeField(default=datetime.now)
<other fields>
Fetch the item from each category with the latest pubdate.
'''