Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

python - Processing django models

I am trying to process a query from my database of model TradeOrder:

class TradeOrder(models.Model):
    #denoted in 2 three letter tickers with underscore such as anc_btc
    pair = models.CharField(max_length=50)
    #buy_order or sell_order
    order_type = models.CharField(max_length=50)
    #number e.g. .3455 or .190  
    trade_rate = models.PositiveIntegerField()
    trade_amount = models.PositiveIntegerField()
    #name of the account
    trade_order_account = models.CharField(max_length=50)

    def __str__(self):
        return '%s %s %s %s %s'  % (self.pair, self.order_type, self.trade_rate, self.trade_amount, self.trade_order_account)

when i execute under views.py for my app

buyorders=TradeOrder.objects.filter(pair="anc_btc", order_type="buy_order")

i get a list that looks like this:

[<TradeOrder: anc_btc buy_order 7987 7897 a>, <TradeOrder: anc_btc buy_order 7897 789 a>, <TradeOrder: anc_btc buy_order 7897 789 a>]

so want to process and refine that data, firstly to compare each item to a new order

something like:

            if new_order_type=="buy_order":
                #crosscheck against sell orders
                market_sell_orders = TradeOrder.objects.filter(pair="anc_btc", order_type="sell_order", trade_order_account=price)
                #now i need to sort the orders for trade rates above/greater than $new_order_price
                #how do i do this?

i now know that this can be accomplished by adding .order_by('trade_order_price') to the end of the query

                if potential_sell_orders is not None:
                #trade
                    do=1
                else: 
                    #no sell orders to fill, submit to order book
                    try:
                        tradeordersubmit=TradeOrder(pair=order_pair, order_type=order_type, trade_rate=price, trade_amount=quantity, trade_order_account=request.user.username) 
                        tradeordersubmit.save()
                        order_error=0
                    except:
                        order_error="1"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
now i need to sort the orders for trade rates above/greater than $new_order_price

You should refine your QuerySet appropriately using QuerySet methods:

 market_sell_orders = TradeOrder.objects.filter(pair="anc_btc", order_type="sell_order", trade_order_account=price, trade_rate__gt=<trade_rate>).order_by('-trade_rate')

You can find the tutorial for QuerySet here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...