One-To-Many Example in NDB
Asked Answered
E

4

7

I am trying to create ndb.Model class like Students and subjects

class Subject(ndb.Model):
     name = ndb.StringProperty()

class Student(ndb.Model):
    name = ndb.StringProperty()
    subject = ndb.KeyProperty(kind=Subject)

One Student can have many Subjects. How to add and store these in this Model. I could not find any example of it. For String Property .. there is field property i.e. repeat=true

How to achieve this and is there any working example on the web. Sorry if it is duplicate question but I tried with my limited skills to search this forum.

Exponible answered 9/4, 2012 at 17:43 Comment(4)
Resolved. Added in the model as : subject = ndb.KeyProperty(kind=Subject,repeated=True) and then when adding the method..in Student object just add std = Student() sub1 = Subject() sub2 = Subject() sub1.put() sub2.put() std.subject.append(sub1) std.subject.append(sub2) std.put()Exponible
Do you know if there is a way to upload repeated properties using bulkloader import transforms?Matchwood
Anjana in your comment with the correction, aren't you supposed to do std.subject.append(sub2.key())?State
@State I think you do std.subject.append(sub2.key)Recondition
C
7

When I need 1 to many I use repeated keyProperties. Code:

class Subject(ndb.Model):
     name = ndb.StringProperty()

class Student(ndb.Model):
    name = ndb.StringProperty()
    subjects = ndb.KeyProperty(kind='Subject', repeated=True)

template:

{% for subject in student.subjects %}
  {{subject.get().name}}
{% endfor %}

ndb is nosql so you will not find reference to the parent in the child. However, you could add it like that. Don't forget to set student key value when creating a new subject.

class Subject(ndb.Model):
     name = ndb.StringProperty()
     student = ndb.KeyProperty(kind='Student')

class Student(ndb.Model):
    name = ndb.StringProperty()
    subjects = ndb.KeyProperty(kind='Subject', repeated=True)
Conscript answered 20/1, 2015 at 13:40 Comment(0)
R
1

Use the subject as a key.

me = Student(key_name='KurzedMetal')
programming = Subject(key_name='Programming')
programming.put()
me.subject = programming.key()
me.put()
Raasch answered 9/4, 2012 at 17:55 Comment(2)
one caveat, to get the name you should use the name() method of the key() (for example: "me.key().name()"), you could add a constructor to save the key_name to the name property if you want.Raasch
Thanks KurzedMetal. Appreciate it. What I am looking for is if there are more than one subjects that the student is associated with. i.e. if Student is associated with Programming and also with DBMS and many other. How can you achieve that?Exponible
R
1

Definition:

class Subject(ndb.Model):
     name = ndb.StringProperty()

class Student(ndb.Model):
    name = ndb.StringProperty()
    subject = ndb.KeyProperty(kind=Subject,repeated=True)

Usage:

subject1 = Subject()
subject1.put()
subject2 = Subject()
subject2.put()
student = Student()
student.subject.append(subject1.key)
student.subject.append(subject2.key)
student.put()
Recondition answered 3/4, 2016 at 21:47 Comment(0)
N
0

This looks like an old question. In case someone else needs this presently, you should look at Structured Properties https://developers.google.com/appengine/docs/python/ndb/properties#structured. The example is very clear and easy to follow.

Ninebark answered 18/12, 2012 at 17:34 Comment(2)
Structured properties don't fill the exact role of a 1-to-many relationship, since you can't relate Addresses (in that example) to anything else.Carolincarolina
That just enforces the structure of a property, it doesn't create a one-to-many relationship.Dak

© 2022 - 2024 — McMap. All rights reserved.