Django aggregate min/max with other fields -
i wounder, can convert sql:
select min(price) min_price, * table group year(date), month(date); with django orm ? need 1 object each month minimum price. tried use aggregate():
model.objects.filter(*filtering*).aggregate(min('price')) but lost other fields, shows 'price' tried, post fields needed:
model.objects.values(*some fields*).filter(*filtering*).aggregate(min('price')) but after didn't field ! price. need object min price , other fields too
have tried use .order_by()?
for example:
yourmodel.objects.all().order_by(min_price)[:1] when want values of model fields can access them using template tags {{ yourmodel.yourmodelfield }}
and if want access them outside of templates have
mymodel = yourmodel.objects.all().order_by(min_price)[:1] print(mymodel.yourmodelfield) the print() example ofcourse
Comments
Post a Comment