Creating a new instance of an entity using ndb has previously interacted with entity's data stored

43 views
Skip to first unread message

Robert Fischer

unread,
Jul 28, 2012, 2:56:46 PM7/28/12
to google-a...@googlegroups.com
Hi,

I'm creating a new instance of a model and it's getting the data from the previous model I created.

Any idea what could be happening here?

class Keyword(ndb.Model):
    user_owner = ndb.UserProperty()
    description = ndb.StringProperty(indexed=False)
    time_created = ndb.DateTimeProperty(auto_now_add=True, indexed=False)
    deleted = ndb.BooleanProperty(default=False)
    keyword_list = ndb.StringProperty(repeated=True, indexed=True) #Indexed
    required_keyword_list = ndb.StringProperty(repeated=True, indexed=False)

    threshold = ndb.IntegerProperty(indexed=False)

    last_found_on_id = ndb.StringProperty(indexed=False)
    ids_found_on = ndb.JsonProperty(default=[], indexed=False)
    times_found = ndb.ComputedProperty(lambda self: len(self.ids_found_on)) #Indexed
    time_last_found = ndb.DateTimeProperty(indexed=False)

class KeywordHandler(webapp2.RequestHandler)
    def post(self):
        ...
        logging.warning('Creating new keyword object.')
        keyword_to_save = Keyword()
        logging.warning('ids associated with keyword: %s %s %s' % (str(keyword_to_save.key), str(keyword_to_save), keyword_to_save.ids_found_on))
        ...

The first time I run this it works great:
WARNING  2012-07-28 18:27:32,403 frontend_keywords.py:176] ids associated with keyword: None Keyword() []

When run a second time I get this in my log file -- why is the ids_found_on already populated for a new model instance? The ids_found_on matches the previously created entity:

WARNING  2012-07-28 18:27:40,459 frontend_keywords.py:176] ids associated with keyword: None Keyword() ['ahBkZXZ-ZGVhbHNjb3JjaGVyciwLEghSU1NFbnRyeSIeMjAxMi0wNy0yNCAxMzo0NTozOC1zeW1hbnRlYyBuDA', 'ahBkZXZ-ZGVhbHNjb3JjaGVyciwLEghSU1NFbnRyeSIeMjAxMi0wNy0yNCAxNjozNDo0Ni1kb2xpY2EgcHJvDA', 'ahBkZXZ-ZGVhbHNjb3JjaGVyciwLEghSU1NFbnRyeSIeMjAxMi0wNy0yNCAxNzowMDowOS10cmVuZG5ldCA4DA', 'ahBkZXZ-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1sb2dpdGVjaCBoDA']

Thanks,
Robert

Kaan Soral

unread,
Jul 28, 2012, 4:35:39 PM7/28/12
to google-a...@googlegroups.com
def GG():
  a=1

def testit(gg=GG()):
  a+=1
  print a

testit(); testit(); testit()

I didn't check your code much but may this be the problem?

Kaan Soral

unread,
Jul 28, 2012, 4:36:24 PM7/28/12
to google-a...@googlegroups.com
Correction


On Saturday, July 28, 2012 11:35:39 PM UTC+3, Kaan Soral wrote:
def GG():
  a=1

def testit(gg=GG()):
  gg.a+=1
  print gg.a

Guido van Rossum

unread,
Jul 28, 2012, 5:42:15 PM7/28/12
to google-a...@googlegroups.com
The "default=[]" is causing this. All instances share the same list object this way. You'll have to find some other way of initializing the value. Maybe you cna just use a repeated string property?

Robert Fischer

unread,
Jul 28, 2012, 6:24:41 PM7/28/12
to google-a...@googlegroups.com
Thanks Guido,

I removed the default argument and things are working as expected.

-Robert

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine/-/F0VkBIsYR2YJ.

To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.

Reply all
Reply to author
Forward
0 new messages