Skip to content Skip to sidebar Skip to footer

How To Assign Default Value To All Ndb Datastore Entries?

I have to add one new property to my existing NDB class: class AppList(ndb.Model): ... ignore = ndb.BooleanProperty(default=False) # new property Then I will use it like b

Solution 1:

Don't forget that there is a MapReduce library that is used in these cases. But I think the best method is to use all these suggestions toghether.

Now, you need to get() and put() 4000 entities and the question is how to reduce the "costs" of this operation.

I'm just curious to know what your bool(entity.ignore) returns. If a missing property return False you can adjust the code considering it False and postponed the operation. If you put() for other reason the property ignore is written to False thanks to the default argument. So, for the rest of the entities can run a script like this (via remote_api):

defiter_entities(cursor=None):
    entries = AppList.query()
    res, cur, more = entries.fetch_page(100, start_cursor=cursor)
    put_queue = [ent for ent in res ifnothasattr(ent, 'ignore')]
    # put_queue = []# for ent in res:#    if not hasattr(ent, 'ignore'):#        put_queue.append(ent)
    ndb.put_multi(put_queue)
    if more:
        iter_entities(cur) # a taskqueue is better

Solution 2:

Your updated code will update first 100 entities only. try using cursor

https://developers.google.com/appengine/docs/python/ndb/queries#cursors

if u cant use cursor then use offset and keep increasing the offset by 100 on every loop or fetch all the entries once by fetch() (cursor approach is better one)

and instead of putting them one by one use ndb.put_multi(list of entities to put)

this will be more faster than putting one by one

Solution 3:

You can try using hasattr to check to see if a record has the ignore property.

If you just want to assign False for all records in your AppList entity, you just need to do an update to your schema (reload the models.py file) and then you should be able to set the property to False.

More information on a schema update can be found here.

EDIT: to answer your comment:

ifhasattr(entity, 'ignore'):
  #yourcodegoeshere

Solution 4:

The simplest way without deploying new code, will be to use the remote api and perform a query fetching all entities and setting the property value to false and and then put() them. 4000 records is not a lot.

In fact you don't even need to explicitly set the value, it will be set when retrieved to the default value if it has no value currently.

Post a Comment for "How To Assign Default Value To All Ndb Datastore Entries?"