I have some entities in the project, objects, such as the User class, the Chat class, and there are documents to which they are attached. Can I expand the document object in any way? That is, now that it costs only the class Chat, I have to use such noodles, but I just simply need to create a chat in the database if it is not there and add one property method.
class Chat(Document): bot = ObjectIdField(required=True) i = LongField(required=True) greeting = BooleanField(default=True, required=True) rules = StringField(min_length=2) silence_ts = LongField(required=True) games = EmbeddedDocumentListField(Game) meta = { 'indexes': [ {"fields": ["bot", "i"], "unique": True} ] } class Chat: def __init__(self, bot, i=0): self.bot = bot self.i = i def __getattr__(self, item): data = modules.Chat.objects(bot=self.bot.id, i=self.i) if not data: data = modules.Chat(bot=self.bot.id, i=self.i).save() else: data = data[0] return getattr(data, item) @property def is_silence(self): return self.silence_ts >= tools.ts() And for that, I pay the inability to update many of the values ​​on the fly. Accounted for instead
user = modules.User.objects.first() # ... user.balance += 1000 Do
User.update(inc__balance=1000) It only seems beautiful, but with large document structures, in this situation, you can just die.