0

I have a GAE datastore with 303 Game() entities.

class Game(db.Model):
    title = db.StringProperty(required = True)
    slug = db.StringProperty(required = True)
    category = db.CategoryProperty()
    description = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)

The 'slug' property is similar to the title except that it replaces all whitespace with a hyphen and removes all special characters. ex: ('The First Game!' becomes 'the-first-game')

On the website I am listing all 303 games using the Bootstrap collapse accordion. It works fine, however because there are so many games, I attempting to setup a side nav that lists the letters of the alphabet. The user would click on one of the letters to jump to the specific position of the list. ex: (User clicks 'N', page jumps to games that start with 'N').

I know this works, because if I hard code the 'slug' into the HTML it will correctly jump to desired location. However, the 'slug' needs to be one before the correct 'slug' or it won't jump correctly. ex: (User clicks 'N', the slug in the HTML needs to be 'my-last-m-game' to jump to the correct position, and display 'N' games at the top of the page).

My question (sorry for the long winded explanation) is, is it possible to do a GQL query to get the last game for each letter of the alphabet? Or is it better to put all the games in a list/dictionary and get the correct 'slug' that way? Or should I add a property to the data itself, that allows me to GQL search just for the 'A' games or just for the 'T' games, to get the correct 'slug'. Or is there an offset I could use in Bootstrap, that would allow me to get the first 'N' game 'slug' and use that to jump to the correct position on the page?

Thanks for any help you can offer. It's my first time posting to stackoverflow, apologies if I'm not doing it correctly.

2 Answers 2

0

Another option is to have a field on Game called lastGameForLetter )which is set to null for most games). When you insert a game, it will require an extra check to make sure this doesn't need to be updated but makes the querying much easier. We do things like this all the time for things that are written rarely but read often.

0

You can use prefix-match on strings. u"\ufffd" is the highest possible unicode-character, so querying by name < u"b"+u"\ufffd" and sorted by name should do the trick for elements starting with "b". Please note that such a filtering/ordering is case-sensitive, so you might need to store an additional field in lowercase for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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