Вопрос

I have a Class entity, which has the properties name, and students.I want to first of all create the entity then add values to the student property afterwards.But i am not sure how to do this.I have tried null (forgive me if this is an abomination, am new to Python) but it returns an error message.This is my Class definition

class Class(ndb.Model):
     name = ndb.StringProperty(required=True)
     students = ndb.KeyProperty(repeated=True) 
     ...

And the handler for my Class Model

class ClassHandler(webapp2.RequestHandler):
def post(self):
    name=self.request.get('class_name')
    callback = self.request.get('callback')
    class = class.get_by_id(name)
    if class:
        self.error(409)
    else:
        n = self.request.get('class_name')
        class=Class(id=n, class_name=n, members) #<<<---//this is where am stuck
        team.put()
        if callback:
            self.response.write(callback + '(' + team.toJSON() + ')')
        else:
            self.response.write(team.toJSON())

I spent the past couple of hours on Google looking for a solution.Any suggestion on what i need to do would be appreciated.

EDIT: This is the error message i get when i try to use null enter image description here

Это было полезно?

Решение

A big +1 to everything @akgill said.

As to your specific problem, you do not need to provide any properties at Entity instantiation, or you can provide some or all.

I am not sure what you are referring to when you are talking about "null" or None.

Any way to add students you need to provide a list of students in the constructor call, or use append

cls = Class()
cls.students.extend([list of student keys])x

Другие советы

  • Okay, first of all class is a protected word in python and you should not be naming your variable class. When you do this, you are locally overwriting the meaning of the word class to Python. Also, you seem to be doing class.get_by_id(name). Do you mean Class.get_by_id(name)? That would be the correct syntax if get_by_id(argument) were a method from your Class class.

  • Secondly, you should post the trace that you get out with the error message in order to get help on your problem.

  • I think you can just declare Class() without any parameters and it will achieve what you are going for. It will create the Class object without any of the parameters specified and you can specify them later.

Example:

class ClassHandler(webapp2.RequestHandler):
def post(self):
    name=self.request.get('class_name')
    callback = self.request.get('callback')
    the_class = Class.get_by_id(name)
    if not the_class:
        self.error(409)
    else:
        n = self.request.get('class_name')
        the_class = Class()
        team.put()
        if callback:
            self.response.write(callback + '(' + team.toJSON() + ')')
        else:
            self.response.write(team.toJSON())

The changes I have made to what you had:

  1. I've changed variable name class to the_class everywhere.
  2. I've changed your instantiation of the variable class so you have Class.get_by_id(name) which is the correct syntax for calling that method, associated with ndb.Model, inherited by Class
  3. I've changed your if statement to if not the_class. Is that what you wanted? Otherwise, I'm confused about what's going on there.
  4. In the else statement, you can see I've just declared the_class = Class() which I think is what you were asking about
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top