views:

53

answers:

1

Hi,

I have a table with countrycodes, whats the best way to store these 2 digit codes, just in a regular property or in the key_name property?

I thought using key_name will produce faster performance?

Ton.

A: 

Whether or not you want to use key_name depends on how you want to use it. I'm not entirely certain how you want to use these country codes based on the information in your question. Let's say, however, that you want to be able to look up the full name of the country based on the two-letter code, for instance, to get "United States" from "us". In that case, you could have an entity like this:

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

    @classmethod
    def lookup_by_code(cls, code):
        country_name = ""
        country = cls.get_by_key_name(code)
        if country is not None:
            country_name = country.name

        return country_name

Now, this code is not complete; you'd need to populate the model with data, and this approach does not allow for reverse lookups -- getting the code when you have the country name, but this should give you an idea of how you might be able to use key_names. Doing it this way would be substantially faster then, say, having a model that contained both code and name as fields.

Adam Crossland