Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 687977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:10:57+00:00 2026-05-14T02:10:57+00:00

First off, I’m relatively new to Google App Engine, so I’m probably doing something

  • 0

First off, I’m relatively new to Google App Engine, so I’m probably doing something silly.

Say I’ve got a model Foo:

class Foo(db.Model):
   name = db.StringProperty()

I want to use name as a unique key for every Foo object. How is this done?

When I want to get a specific Foo object, I currently query the datastore for all Foo objects with the target unique name, but queries are slow (plus it’s a pain to ensure that name is unique when each new Foo is created).

There’s got to be a better way to do this!

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:10:57+00:00Added an answer on May 14, 2026 at 2:10 am

    I’ve used the code below in a project before. It will work as long as the field on which you’re basing your key name on is required.

    class NamedModel(db.Model):
        """A Model subclass for entities which automatically generate their own key
        names on creation. See documentation for _generate_key function for
        requirements."""
    
        def __init__(self, *args, **kwargs):
            kwargs['key_name'] = _generate_key(self, kwargs)
            super(NamedModel, self).__init__(*args, **kwargs)
    
    
    def _generate_key(entity, kwargs):
        """Generates a key name for the given entity, which was constructed with
        the given keyword args.  The entity must have a KEY_NAME property, which
        can either be a string or a callable.
    
        If KEY_NAME is a string, the keyword args are interpolated into it.  If
        it's a callable, it is called, with the keyword args passed to it as a
        single dict."""
    
        # Make sure the class has its KEY_NAME property set
        if not hasattr(entity, 'KEY_NAME'):
            raise RuntimeError, '%s entity missing KEY_NAME property' % (
                entity.entity_type())
    
        # Make a copy of the kwargs dict, so any modifications down the line don't
        # hurt anything
        kwargs = dict(kwargs)
    
        # The KEY_NAME must either be a callable or a string.  If it's a callable,
        # we call it with the given keyword args.
        if callable(entity.KEY_NAME):
            return entity.KEY_NAME(kwargs)
    
        # If it's a string, we just interpolate the keyword args into the string,
        # ensuring that this results in a different string.
        elif isinstance(entity.KEY_NAME, basestring):
            # Try to create the key name, catching any key errors arising from the
            # string interpolation
            try:
                key_name = entity.KEY_NAME % kwargs
            except KeyError:
                raise RuntimeError, 'Missing keys required by %s entity\'s KEY_NAME '\
                    'property (got %r)' % (entity.entity_type(), kwargs)
    
            # Make sure the generated key name is actually different from the
            # template
            if key_name == entity.KEY_NAME:
                raise RuntimeError, 'Key name generated for %s entity is same as '\
                    'KEY_NAME template' % entity.entity_type()
    
            return key_name
    
        # Otherwise, the KEY_NAME is invalid
        else:
            raise TypeError, 'KEY_NAME of %s must be a string or callable' % (
                entity.entity_type())
    

    You could then modify your example model like so:

    class Foo(NamedModel):
        KEY_NAME = '%(name)s'
        name = db.StringProperty()
    

    Of course, this could be dramatically simplified in your case, changing the first line of the NamedModel‘s __init__ method to something like:

    kwargs['key_name'] = kwargs['name']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I am pretty new to C, so I probably just have a
First off, I would like to make clear, that I am SUPER NEW TO
First off tags wont work. I say this because i create 4 buttons all
First off, I did look on google and stackoverflow, but nothing seem to really
First off, a big thanks to the people behind the new boost::geometry library! This
First off, I will say i've spent 6 hours on this topic and have
First off, I'm pretty new so I hope I hadn't missed anything too trivial.
First off, a lil about me, i'm very new to GUI programming, especially with
First off I have an app that provides the user information about the amount
First off im very new to C# im trying to recreate a application i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.