0
class User(db.Document):
    email = db.StringField(required=True)
    first_name = db.StringField(max_length=50)

    ref = db.ReferenceField('Post')



class Post(db.Document):
    title = db.StringField(max_length=120, required=True)
    tags = db.ListField(db.StringField(max_length=30))

I have two classes User and Post. I want to access the elements of User class from Post class(ref) using:

User.objects.first().ref.title

Error:

AttributeError: 'NoneType' object has no attribute 'title'

How do I do it? How to access all elements and not just one. Thanks in advance.

1 Answers1

0

I don't believe you can do that with a reference field, you would have to take the reference and find the object via:

Post.objects(id=User.objects().first().ref["id"]).first().title

You could though if you made Post an EmbeddedDocument of User and you call for the User objects with select_related like:

User.objects().first().select_related(1).ref.title

Or you could overwrite the json interpretation as explained here: https://stackoverflow.com/a/23990828/493685

grafuls
  • 311
  • 5
  • 8