def master_model(options): """ The Master model class of this Model's Master/Detail relationship. Accessible at ``<model_class>._meta.master_model``, the Master model class in a Master/Detail relationship is the most generic non-abstract Model in this model's multiple-table chain of inheritance. If this model is not a detail model, None will be returned. """ # If this isn't even a MasterModel descendant, don't bother. if not issubclass(options.model, MasterModel): return None try: # The last item in this list is the oldest ancestor. Since the MasterModel usage # is to declare your master by subclassing MasterModel, and MasterModel is abstract, # the oldest ancestor model is the Master Model. return options.get_parent_list()[-1] except IndexError: # Also None if this model is itself the master. return None