-1

I am trying to build a reminder app using google app engine and python ndb. Now i have two entities User and Reminder. What would be a better design:

  1. Have Reminder as a separate entity and have a list of reminders set by User and reminders for user in the User entity. Mark a new reminder with a unique id and just add its id appropriately in the User entity.

OR

  1. Have reminders as a property of User class. Add new reminders to directly to the users. In this case there would be two copy of the reminder one in the user who set it and other in, for whom it was set.

If I go with the first one, would an integer id incremented for each reminder suffice, considering it could go very high? Or is there some other better way to do this.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
vigenere
  • 197
  • 3
  • 15

1 Answers1

0

The Google App Engine (GAE) NDB Model Key ID numbers don't auto-increment, so I'm not sure that's a relevant question.

GAE NDB models use keys, which can be stored as key property lists in other model instances. The keys store the ids, which can be used to retrieve the reminders later.

#  Create an instance of a model
instance = SomeModel()
instance.put()

# Retrieve ID
instance.key.id()

For example, you could have reminder and user models, but have the reminder keys as a property of users:

from google.appengine.ext import ndb 

class Reminder(ndb.Model):
    remindTime = ndb.DateTimeProperty()

class User(ndb.Model):
    reminderKeys = ndb.KeyProperty(kind=Reminder, repeated=True)

When adding new reminders to a user, you would do the following:

from datetime import datetime, timedelta

# Set reminder times to future
reminder1 = Reminder(remindTime = datetime.now() + timedelta(minutes = 10))
reminder2 = Reminder(remindTime = datetime.now() + timedelta(minutes = 20))

# Send reminders to datastore to assure they have generated keys
reminder1.put()
reminder2.put()

# Create user with reminder1
user = User(reminderKeys = [reminder1.key])

# Add reminder2 to user
user.reminderKeys.append(reminder2.key)

# Retrieve Reminders for user
for key in user.reminderKeys:
    print Reminder.get_by_id(key.id())

This way you can create other properties for the reminder, while still tying them to the User model.

You can read more on how GAE NDB properties and keys behave here: https://developers.google.com/appengine/docs/python/ndb/properties

Jabberwockey
  • 493
  • 3
  • 17