python - django 1.4 : I get an error in queryset extra -
python - django 1.4 : I get an error in queryset extra -
i have table table_object_price
. fields: field_sale_currency_value
name of currency, field_sale_whole_value
name of price.
class object(node): id = models.onetoonefield('nod', parent_link=true, db_column='id') name = models.textfield(blank=true, null=true) field_sale_currency_value = models.textfield(blank=true, null=true) field_sale_whole_value = mintegerfield(blank=true, null=true)
this sql code makes sorting:
select *, case field_sale_currency_value when 'rur' field_sale_whole_value/40 when 'usd' field_sale_whole_value when 'eur' field_sale_whole_value/0.7423 field_sale_currency_value welhome.welhome_content_type_object order cost_usd ;
but need utilize django because function gets queryset:
def sort_queryset(queryset): queryset = queryset.extra(select={'cost_usd':'(case field_sale_currency_value when "rur" field_sale_whole_value/40 when "usd" field_sale_whole_value when "eur" field_sale_whole_value/0.7423 end)'}, order_by=["cost_usd"]) homecoming queryset
and not work.
error cannot resolve keyword 'cost_usd' field
from django docs:
if need order resulting queryset using of new fields or tables have included via extra()
utilize order_by parameter extra()
, pass in sequence of strings. these strings should either model fields (as in normal order_by()
method on querysets), of form table_name.column_name
or alias column specified in select parameter extra()
.
so, need as:
queryset = queryset.extra(select={'cost_usd':'(case field_sale_currency_value when "rur" field_sale_whole_value/40 when "usd" field_sale_whole_value when "eur" field_sale_whole_value/0.7423 end)'}) queryset = queryset.extra(order_by = ['cost_usd'])
python mysql django orm
Comments
Post a Comment