0

I'm following the instruction from an answer, why is my object that is returned by the function None and not an entity?

class FileInfo(db.Model):

    blob = blobstore.BlobReferenceProperty(required=True)
    randomvalue = db.FloatProperty()
    uploaded_by = db.UserProperty()
    facebook_user_id = db.StringProperty()
    uploaded_at = db.DateTimeProperty(required=True, auto_now_add=True)
    category = db.CategoryProperty(choices=('eyes', 'nose', 'mouth',
                                   'other'))

# snip -- shortened here

    def get_random_image(self, category):
    """
        fileinfos = FileInfo.all().filter('category =', category)
        return fileinfos[random.randint(0, fileinfos.count() - 1)]
    """

        q = FileInfo.all()
        q.filter('category =', category)
        q.filter('random >=', random.random())
        return q.get() # why is this not working?
5
  • 1
    Your title provided no information at all about your problem. I guessed at what you were trying to ask; please try and make your question titles more informative in the future.
    – agf
    Sep 28, 2011 at 16:22
  • 1
    To the close-voters: Yes, this question is localized, but it's far from the first person I've seen mystified by empty result sets because of a typo. I think this question will be useful to others. Sep 28, 2011 at 23:32
  • @Nick Johnson I think the VM should have told me that the variable doesn't exist? But I'm still learning python. Sep 29, 2011 at 6:30
  • @agf Thanks for the help! I get very good help here so naturally I'll follow all recommendation I can. I'm here for recommendations. Sep 29, 2011 at 6:43
  • 1
    No - your query criteria is a string, so the Python runtime doesn't parse it. Sep 29, 2011 at 8:46

1 Answer 1

3
q.filter('random >=', random.random())

should be corrected in:

q.filter('randomvalue >=', random.random())
2
  • Assuming the randomvalue field is actually populated with roughly uniformly distributed floats (random floats is fine if there are a large number of entries) in the range [0.0, 1.0).
    – agf
    Sep 28, 2011 at 16:28
  • 1
    It is populated (I did it with mapreduce just assigning random.random()) and of course the variable should be randomvalue. Thanks a lot for proving that I'm blind. Sep 28, 2011 at 17:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.