I'm using a model as follows:
# Creating data models for the interns first project.
class Sex(db.Document):
_id = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
gender = db.StringField()
class Address(db.EmbeddedDocument):
_id = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
number = db.IntField()
street = db.StringField()
city = db.StringField()
eircode = db.StringField()
class Person(db.Document):
_id = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
name = db.StringField()
sex = db.ReferenceField(Sex, required=True)
address = db.ListField(db.EmbeddedDocumentField(Address))
I already have the values created as documents in 'sex' collection, I want to reference to one of those values on the insertion of a new document in 'person' collection. (As far as I understood that would be a one to many relationship)
That's how far I got but that doesn't work as expected:
if Person(name='USERNAME', sex=Sex(_id='602a593d3149eb362c296561', gender='Male'),
address=[Address(number=1, street='STREET 1', city='CITY 1', eircode='EIRCODE 1'),
Address(number=2, street='STREET 2', city='CITY 2', eircode='EIRCODE 2')]
).save():
return 'Successfully added'
return 'Error in Register'
Found out that I was getting an error for setting default values on the oid (_id) field in person, worked fine after removing it.
# Creating data models for the interns first project.
class Sex(db.Document):
_id = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
gender = db.StringField()
class Address(db.EmbeddedDocument):
_id = ObjectIdField(required=True, default=ObjectId, unique=True, primary_key=True)
number = db.IntField()
street = db.StringField()
city = db.StringField()
eircode = db.StringField()
class Person(db.Document):
_id = ObjectIdField()
name = db.StringField()
sex = db.ReferenceField(Sex, required=True)
address = db.ListField(db.EmbeddedDocumentField(Address))