views:

561

answers:

1

Okay so I have this mode:

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

and this 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))

So when an entity is added a random float is generated (0-1) and then when I need to grab a random entity I want to be able to just use a simple SELECT query. It errors with:

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

Now this works if I go:

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

So clearly my query is wrong; how does one use a variable in a where statement :S

+2  A: 

Substitute:

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

with:

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

Or

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

See for more information: "The Gql query class"

The funny thing is that I have just learned this about 2 hrs ago :P

OscarRyz
Yay, thanks - that's fixed it :P Though it's actually:WHERE rand1 > :1 ORDER BY rand1 LIMIT 1", rand2)I accidentally had 'rand' instead of 'rand1', incase others were wondering and needed help.
Dominic Bou-Samra