Reputation: 1343
I'm using Python and GAE, and I'm trying to create a dictionary with keys being the userid and values being a 'Student' object. However, my dictionary's values are None rather than a student object.
{'60': , '59': }
I would really appreciate it if someone could point me in the right direction!
Student.py
class Student:
def __init__(self, name, s_id, rew = {}):
self.name = name.strip()
self.rewards = {"Creativity": 0, "Helping Others":0, "Participation":0, "Insight":0}
self.totalRewardPoints = 0
self.s_id = s_id
Main.py (I've only included the relevant code)
class PageHandler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
def create_students(self):
user = db.GqlQuery("SELECT * FROM User WHERE position='student'")
for u in user:
temp_id = str(u.key().id())
self.students[temp_id] = student.Student(u.name, temp_id)
class MainPage(PageHandler):
students = {}
def get(self):
user = db.GqlQuery("SELECT * FROM User WHERE position='student'")
for u in user:
temp_id = str(u.key().id())
self.students[temp_id] = student.Student(u.name, temp_id)
self.write(self.students)
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
Upvotes: 1
Views: 251
Reputation: 12986
For starters you Student class needs to inherit from some model class implementing appengine datastore persistence. If you you are using the original datastore api then db.Model, if ndb then nbd.Model.
Secondly you haven't shown how you write (put()) student entities to the datastore. Based on the fact you are not inheriting from either (db or ndb) it's unlikely you are saving anything to the datastore.
Unless of course you are not including your actual code. If you are using db.Model as the base class, then your rewards field is not going to work. You should probably look at ndb as an alternate starting point and use a Structured Property.
You probably need to read up on the appengine storing data over view doc https://developers.google.com/appengine/docs/python/datastore/overview#Python_Datastore_API you code looks nothing like GAE (Google Appengine code)
You student class should look something like (if you want a rewards field to have some structure)
class Reward(ndb.Model):
reward = ndb.StringProperty()
value = ndb.IntegerProperty()
class Student(ndb.Model):
name = ndb.StringProperty(required=True)
rewards = ndb.StructuredProperty(Reward, repeated=True)
total_reward_points = ndb.IntegerProperty()
s_id = ndb.StringProperty()
otherwise if you use db.Model then rewards would be a db.BlobProperty() and you would then pickle is use json to encode rewards dictionary using pickle.dumps or json.dumps when saving data.
Upvotes: 2