Domanda

Va bene così ho questo modo:

class Posts(db.Model):
  rand1 = db.FloatProperty()
  #other models here

e questo controller:

class Random(webapp.RequestHandler):
  def get(self):    
      rand2 = random.random()
      posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
      #Assigning values for Django templating
      template_values = {
          'posts_query': posts_query,
           #test purposes
          'rand2': rand2,
          }

      path = os.path.join(os.path.dirname(__file__), 'templates/random.html')
      self.response.out.write(template.render(path, template_values))

Così, quando un soggetto viene aggiunto un galleggiante a caso è generato (0-1) e poi quando ho bisogno di afferrare un'entità a caso io voglio essere in grado di utilizzare solo una semplice query SELECT. gli errori con:

BadArgumentError('Missing named arguments for bind, requires argument rand2',)

Ora, questo funziona se vado:

posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")

Quindi, chiaramente la mia domanda è sbagliata; come si fa a utilizzare una variabile in una dichiarazione in cui: S

È stato utile?

Soluzione

Sostituire:

 "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")

con:

  "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1", rand2=rand2)

o

  "...WHERE rand1 > :1 ORDER BY rand LIMIT 1", rand2)

Si veda per maggiori informazioni: " La classe di query GQL "

La cosa divertente è che ho appena imparato a circa 2 ore fa: P

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top