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 8682719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:51:02+00:00 2026-06-12T21:51:02+00:00

I’m trying to show a table of ~800 entities, and having problems keeping it

  • 0

I’m trying to show a table of ~800 entities, and having problems keeping it from being really slow. (Like 15-20 seconds slow.) I successfully implemented memcache, but because I reference a parent model for each of the child entities it still causes a datastore_v3.Get for each of the 800 and is massively slow.

I then implemented Nick Johnson’s ReferenceProperty prefetching and can’t solve the following error:

[... snipped ...]
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
  return method(*args, **kwargs)
File "/myurl/mypythoncode.py", line 67, in get
  prefetch_refprops(entitylist, ChildModel.parent_program.name)
File "/myurl/mypythoncode.py", line 36, in prefetch_refprops
  fields = [(entity, prop) for entity in entities for prop in props]
TypeError: 'NoneType' object is not iterable

Models:

These are the two relevant models:

class ParentModel(db.Model):
  name = db.StringProperty()
  # currently 109 of these

class ChildModel(db.Model):
  name = db.StringProperty()
  parent_program = db.ReferenceProperty(ParentModel)
  website = db.StringProperty()
  # currently 758 of these

Python code:

In my Python code I’m using Nick Johnson’s techniques of efficient model memcaching and for ReferenceProperty prefetching. (I’ve included the ReferenceProperty prefetching below, but not the memcaching code.)

class AllEntities(webapp2.RequestHandler):
  def get(self):
    entitylist = deserialize_entities(memcache.get("entitylist"))
    entityref = prefetch_refprops(entitylist, ChildModel.parent_program.name)
    if not entitylist:
      entitylist = ChildModel.all().fetch(None)
      entityref = prefetch_refprops(entitylist, ChildModel.parent_program.name)
      memcache.set("entitylist", serialize_entities(entitylist))
    context = {
      'entitylist': entitylist,
    }
    self.response.out.write(template.render(context))

def prefetch_refprops(entities, *props):
    fields = [(entity, prop) for entity in entities for prop in props]
    ref_keys = [prop.get_value_for_datastore(x) for x, prop in fields]
    ref_entities = dict((x.key(), x) for x in db.get(set(ref_keys)))
    for (entity, prop), ref_key in zip(fields, ref_keys):
        prop.__set__(entity, ref_entities[ref_key])
    return entities

Jinja2 template:

My Jinja2 template references the iterable “entry” in “entitylist” but also the parent_program.name and parent_program.key().id()

{% for entry in entitylist %}
  <tr>
    <td><a href="{{ entry.website}}">{{ entry.website }}</a></td>
    <td><a href="/urlcategory/view?entityid={{ entry.parent_program.key().id() }}">{{ entry.parent_program.name }}</td>
  </tr>
{% endfor %}

I’ve replaced the line:

entityref = prefetch_refprops(entitylist, ChildModel.parent_program.name)

with

entityref = prefetch_refprops(entitylist, ChildModel.parent_program)

and other variations that include “.name” and “.key().id()”. When I use “.key().id()” I get the error:

AttributeError: 'ReferenceProperty' object has no attribute 'key'

What am I missing or screwing up? I’d really appreciate any help!

  • 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-06-12T21:51:03+00:00Added an answer on June 12, 2026 at 9:51 pm

    Jed, you’re doing it right 🙂

    Two improvements:

    1. You don’t need to assign the return value of the prefetch as it isn’t being used and the companylist will be being modified in place.
    2. I use a slightly modified version of the prefetch_refprops to handle cases where the reference property isn’t populated.

      def prefetch_refprops(entities, *props):
          fields = [(entity, prop) for entity in entities for prop in props]
          ref_keys_all = [prop.get_value_for_datastore(x) for x, prop in fields]
          ref_keys = [ref_key for ref_key in ref_keys_all if ref_key is not None]
          ref_entities = dict((x.key(), x) for x in db.get(set(ref_keys)))
          for (entity, prop), ref_key in zip(fields, ref_keys_all):
              if ref_key and ref_entities[ref_key]:
                  prop.__set__(entity, ref_entities[ref_key])
              else:
                  prop.__set__(entity, None)
          return entities
      

    We’re using this in production code, it makes a real difference. Below is an example of turning on/off the prefetch(es) on a bit of code that is building template values.

    (run:   real_time, Get #rpcs, RunQuery #rpcs)
    Before:   5044 ms,       132,    101
    After:    2214 ms,        53,     11
    

    The other heavy chain-ladder operation our code is doing is a count() on the ref_set for each object, which we will replace in the near future with caching the values on the objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.