0
votes

I am creating an application using Python (2.7) and GAE. I am trying to create a one-to-many relationship. There is one client with numerous properties which also has many potential contacts. Contacts also has various properties. The example of using the ndb.StructuredProperty seems pretty straight forward, but when I import my data model with a structured property line, I keep getting the following error in my log:

NameError: Name 'Contact' is not defined.

Any help would be greatly appreciated.

main.py

from dataObjects import *

dataObjects.py

class Client(ndb.Model):
    createDate = ndb.DateProperty()
    name = ndb.StringProperty()
    address1 = ndb.StringProperty()
    address2 = ndb.StringProperty()
    state = ndb.StringProperty()
    zipCode = ndb.StringProperty()
    phone = ndb.StringProperty()
    fax = ndb.StringProperty()
    website = ndb.StringProperty()
    city = ndb.StringProperty()
    industry = ndb.StringProperty()
    status = ndb.StringProperty()
    notes = ndb.StringProperty()
    financing = ndb.StringProperty()
    contacts = ndb.StructuredProperty(Contact, repeated=True)

class Contact(ndb.Model):
    firstName = ndb.StringProperty()
    lastName = ndb.StringProperty()
    role = ndb.StringProperty()
    status = ndb.StringProperty()
    phone = ndb.StringProperty()
    fax = ndb.StringProperty()
    email = ndb.StringProperty()
    createDate = ndb.DateProperty()
    isClient = ndb.StringProperty()
    address = ndb.StringProperty()
3
Because it's not defined yet. Swap the order of the models.Daniel Roseman
That simple? What a noob! Worked like a charm. Thanks for the super speedy response.Sanders

3 Answers

0
votes

As Daniel Roseman pointed out :

"Because it's not defined yet. Swap the order of the models."

Basically, when creating the model Client, your code needs a Contact object. Since Contact doesn't exist for your code, it breaks.

0
votes

Also, for cases when its not possible to just change the order of definition you can add a property after the model were defined (that includes self-referencing):

class User(ndb.Model):
  pass

User.friends = ndb.StructuredProperty(User, repeated=True)
User._fix_up_properties()
0
votes

You have to swap the order of the 2 models as when you defined the Client model the Contact model is not defined.