Copy from mongoengine import *
connect(db='RelationExample')
যখন EmbeddedDocument কোন ক্লাস এ ব্যবহার করবো তখন ঐক্লাসের অবজেক্ট save মেথড দ্বারা ডেটাবেজ এ সেভ হবে না আমরা শুধু মাত্র অবজেক্ট তৈরী করে তা অন্য একটি অবজেক্ট এর রেফারেন্স হিসাবে সেট করতে পারি।
Copy class Book(EmbeddedDocument):
title = StringField()
pages = IntField()
price = DecimalField()
class Publisher(Document):
name=StringField()
email=EmailField()
books = ListField(EmbeddedDocumentField(Book))
book1 = Book(title='Python Basic',pages=200,price=250.0)
book2 = Book(title='Python OOP',pages=250,price=265.0)
book3 = Book(title='Python Crud',pages=350,price=390.0)
publisher= Publisher(name='Panjery',email='panjery@gmail.com',books=[book1,book2,book3])
publisher.save()
Copy class Book1(Document):
title = StringField()
pages = IntField()
price = DecimalField()
class Publisher1(Document):
name=StringField()
email=EmailField()
books = ListField(ReferenceField(Book1))
book01 = Book1(title='Python Basic',pages=200,price=250.0).save()
book02 = Book1(title='Python OOP',pages=250,price=265.0).save()
book03 = Book1(title='Python Crud',pages=350,price=390.0).save()
publisher=Publisher1(name='Panjerry',email='olee.techs@gmail.com',books=[book01,book02,book03]).save()
Copy class Book2(Document):
title = StringField()
pages = IntField()
price = DecimalField()
publisher=ReferenceField('Publisher2')
class Publisher2(Document):
name=StringField()
email=EmailField()
books = ListField(ReferenceField(Book2))
publisher2=Publisher2(name='Panjerry',email='olee.techs@gmail.com').save()
book201 = Book2(title='Python Basic',pages=200,price=250.0,publisher=publisher2)
book202 = Book2(title='Python OOP',pages=250,price=265.0,publisher=publisher2)
book203 = Book2(title='Python Crud',pages=350,price=390.0,publisher=publisher2)
book201.save()
book202.save()
book203.save()
Copy book201.publisher.name
#output
'Panjerry'