2011-09-15 46 views
0

請參閱my previous question。我根據Kevin P的回答更新了代碼,但現在我無法將最新的標籤排在最前面。我的意見如下query.order("date")。你知道爲什麼帶有減號的日期有效,但沒有減號不起作用嗎?按日期排序時的PropertyError(Google App Engine - Python)

我的模式是:

class Owner(db.Model): 
    owner = db.UserProperty() 
    owner_tag = db.StringProperty() 
    tag_value = db.IntegerProperty() 
    url = db.StringProperty()  

處理程序,提請標籤表:

class UserAdminPage(webapp.RequestHandler): 
    def get(self): 
     order_by = self.request.get("order")  
... 

#-----------tags table-----------------# 
      query = Owner.all() 
      query.filter("owner =", user) 
      if not order_by: 
       query.order("owner_tag") 
#-----------when both "date" has a minus sign the table is sorted correctly 
#-----------when I remove the minus signs I get `PropertyError: Invalid property name 'date'` 
      elif order_by == "-date": 
       query.order("-date") 
      w = query.fetch(500) 

      user_tag_list = [] 
      for item in w: 
       user_tag_list.append(item.owner_tag) 

#   unique_tags = sorted(f1.f2(user_tag_list))     
      unique_tags = f1.f2(user_tag_list) 

#-----------holding table start--------# 
      self.response.out.write(""" 
      <table border="0" cellpadding="0" cellspacing="20" > 
      <tr> 
      <td>""") 

#-----------tags table start--------#         
      self.response.out.write("""<table border="1"> 
      <tr> 
      <th colspan="3">tags<br /> 
      <a href="/useradminpage?order=date"><span id=small>most recent</span></a></th> 
      </tr> 
      """)  

      for tag in unique_tags: 

       self.response.out.write(""" 
        <tr> 
        <td><a href="/tag?tag=%s">%s</a></td> 
        </tr> 
        """ % 
        (tag, tag) 
        ) 

      self.response.out.write("""</table>""") 

... 

回答

3

現在你所依賴的事實,通常按鍵順序生成,這是情況並非總是如此。添加日期屬性到您的模型:

date= db.DateTimeProperty(auto_now_add=True) 

也修復您的GET參數,以便它是一致的。在這裏:

<a href="/useradminpage?order=xyz"><span id=small>most recent</span></a> 

這裏:

elif order_by == "xyz": 
    query.order("date") 
+0

太好了!謝謝。現在所有的作品。 – Zeynel

+0

很高興幫助:) –