code: def serialize(self, q):
          
          children = []
          for child in q.children:
              if isinstance(child, Q):
                  children.append(self.serialize(child))
              else:
                  children.append(child)
          serialized = q.__dict__
          serialized['children'] = children
          return serialized
docs: Serialize a Q object into a (possibly nested) dict.