Python / Django Queries - Append Raw SQL To Model Object -
i want add raw sql django model object. query in sql perform without writing entire thing. raw sql is:
select * elements order if(elements.order=0, 99999, elements.order) asc
basically, order elements order field if order value '0' ordered last.
i have tried using extra() or appending raw() without great success... want able this:
elements.objects.all().extra("order if(order=0, 99999, order)") ## or elements.objects.all().raw("order if(order=0, 99999, order)")
any clue???
don't that.
do this.
results = list( element.objects.filter(whatever).exclude( order__isnull=true ).order_by( order ) ) results.extend( element.objects.filter(whatever).filter( order__isnull=false ) )
that avoid overly complex sql.
try this. it's fast. may faster sql.
def by_order( item ): return item.order if item.order not none else 2**32 results = list( element.objects.filter(whatever).all() ) results.sort( key=by_order )
Comments
Post a Comment