如何使用Google App Engine的ndb.KeyProperty

时间:2014-04-18 09:35:39

标签: python google-app-engine app-engine-ndb

我正在尝试开发一个应用,其中Student个实体通过其键添加到Class实体。所以在课堂上,我可以让一个List的学生通过他们的钥匙识别出来,可以是Integer或String.But我不知道怎么做。(请忍受我,这是我的第一个项目使用Python) 这是我的代码:

import webapp2
from webapp2_extras import json
from google.appengine.ext import ndb

registrationPage = """
<html>
<head><title>Student Registration</title><head>
<body>
<h1>Student Registration</h1>
<form action="/" method="post">
<table border="1">
<tr><td>Srudent's BannerId</td><td><input type="number" size="6" name="banner_id"/></
 td></tr>
<tr><td>Student's Name</td><td><input type="text" size="50" name="name"/></td></tr>
<tr><td><input type="submit" name="submit" value="Register Student"/></td></tr>
</table>
</form>
</body>
</html>
"""
class MathClass (ndb.Model):
class_Id = ndb.IntegerProperty(required=True)
professor_name = ndb.StringProperty(required=True)
number_of_students = ndb.IntegerProperty()
students = ndb.KeyProperty(repeated=True)



# A Class can return its data in JSON format
def toJSON(self):
    jsonData = {
        "class_id": self.key.id(),
        "teacher": self.teacher,
        "number_of_students": str(self.number_of_students),
    }
    return json.encode(jsonData)



 class Student(ndb.Model):
     banner_id = ndb.IntegerProperty(required=True)
     name=ndb.StringProperty()

 # A Student can return its data in JSON format
def toJSON(self):
    jsonData = {
        "banner_id": self.key.id(),
        "name": self.name,
    }
    return json.encode(jsonData)

    def toTableRow(self, points):
        return "<tr><td>" +  self.key.id() + "</td><td>" + self.name + "</td></tr>"

class StudentHandler(webapp2.RequestHandler):
def post(self):
    banner_id = self.request.get('banner_id')
    callback = self.request.get('callback')
    student = Student.get_by_id(banner_id)
    if student:           # This student name already exists.
        self.error(409)  # This is the HTTP status code for 'unable to process due to conflict'
    else:
        id = int(self.request.get('banner_id'))
        i = int(self.request.get('banner_id'))
        n = self.request.get('name')
        student = Student(id=banner_id, banner_id = id, name=n)
        student.put()
        if callback:
            self.response.write(callback + '(' + student.toJSON + ')')
        else:
            self.response.write(student.toJSON())


       class RegistrationHandler(webapp2.RequestHandler):

            def get(self):
            self.response.write(registerationPage)

         app = webapp2.WSGIApplication([
           ('/', StudentHandler),
           ('/reg', RegistrationHandler)
         ], debug=True)

使用HTML表单,我已经能够在我的本地数据存储中保存Student实体。这就是我陷入困境的地方,我如何创建一个以我新创建的学生为成员的类实体那个班级?

1 个答案:

答案 0 :(得分:6)

只需获取或创建类实例,然后将学生键附加到学生属性

  subject = MathClass.get_or_insert(key_name="math_101")
  subject.students.append(some_student.key)
  subject.put()

您应该检查学生是否已经入住。 例如if not student.key in subject.students

很多错误检查等......需要进入这里。我会将该代码作为类的方法,即MathClass.enroll,因此您只需调用subject.enroll(student)