I'm trying to define a Document class that has some fields that have their values automatically calculated when a new document is created.
So far I have succeeded in doing that by extending the __init__
function like this:
class URLDoc(Document):
url = fields.URLField(requierd=True)
domain = fields.StringField(required=True)
screenshot = fields.ImageField(required=True, size=(600, 600, True), thumbnail=(100, 100, True))
def __init__(self, *args, **kwargs):
super(URLDoc, self).__init__(*args, **kwargs)
self.domain = urlparse(self.url).netloc
self.get_and_save_screenshot()
def get_and_save_screenshot(self):
'''Some code to get a screenshot of the website on self.url'''
self.screenshot.put(screenshot_file)
self.save()
This way I was able to create new documents in mongo by just calling new_urldoc = URLDoc(url="some url")
and then just new_urldoc.save()
.
When I started loading existing documents from mongo by URLDoc.objects.get(id="some id")
I realized that the __init__
function will be triggered again set new data (take a new screenshot for example) in the document.
I want to implement this but only when the document is new, I looked up everywhere and couldn't find an answer..
Is there a way to call certain functions when initializing a new document as opposed to initializing an existing document?
Based on mongoengine's documentations, You can make sure a document is being loaded from the database if the _created
kwarg is passed as True
like this:
def __init__(self, your_var, *args, **kwargs):
super().__init__(*args, **kwargs)
print(kwargs)
if not kwargs.get('_created', True):
return # Loaded from DB, No modification needed
self.some_var = your_var
# Initiate the brand new instance
note: this method that's been mentioned before does work, but only for Documents. EmbeddedDocuments do not have the id
attribute!
if self.id:
print("loaded from DB")
else:
print("Brand new instance!")