, self._reverse_name = rel_model_class , reverse_name class ReverseRelation : def __init__(self, origin_model, field_name): self._origin_model, self._field_name = origin_model, field_name def __get__(self, obj, type=None): return self._origin_model.S.filter(self._field_name=obj) class ModelMeta(type): def __new__(cls, name, bases, attrs): type_new = type(name, bases, attrs) for field_name, field in attrs.items(): if isinstance(field, Relation): setattr(field._rel_model_class , self._reverse_name, ReverseRelation (type_new, field_name)) return type_new