문제

Am trying to develop an app where Student entities are added to a Class entity by their keys. So in a class, i can have a List of students identifiable by their keys which could be and Integer or a String.But i have NO idea how to do this.(please bear with me, this is my first project using Python) Here is my code:

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)

Using the HTML form, i have been able to save a Student entity in my local datastore.This is where i become stuck, how do i go about creating a class entity with my newly created student as a member of that class?

도움이 되었습니까?

해결책

Just fetch or create the class instance, then append the students key to the students property

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

You should really check if the student is already in the property. e.g. if not student.key in subject.students

Lots of error checking, etc... needs to go in here. and I would make that code a method of the class, ie MathClass.enroll, so you can just call subject.enroll(student)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top