def model_dependencies(model, checked_models=None):
 depends = set()
 checked_models = (checked_models or set())
 for field in (model._meta.fields + model._meta.many_to_many):
  depends.update(field_dependencies(field, checked_models))
 for base in model.__bases__:
  if (issubclass(base, models.Model) and hasattr(base, '_meta') and (not base._meta.abstract)):
   depends.add(base)
 new_to_check = (depends - checked_models)
 while new_to_check:
  checked_model = new_to_check.pop()
  if ((checked_model == model) or (checked_model in checked_models)):
   continue
  checked_models.add(checked_model)
  deps = model_dependencies(checked_model, checked_models)
  for dep in deps:
   if ((dep not in depends) and (dep not in new_to_check) and (dep not in checked_models)):
    new_to_check.add(dep)
   depends.add(dep)
 return depends

def model_dependencies ( model , checked_models = None ) : depends = set ( ) checked_models = ( checked_models or set ( ) ) for field in ( model . _meta . fields + model . _meta . many_to_many ) : depends . update ( field_dependencies ( field , checked_models ) ) for base in model . __bases__ : if ( issubclass ( base , models . Model ) and hasattr ( base , '_meta' ) and ( not base . _meta . abstract ) ) : depends . add ( base ) new_to_check = ( depends - checked_models ) while new_to_check : checked_model = new_to_check . pop ( ) if ( ( checked_model == model ) or ( checked_model in checked_models ) ) : continue checked_models . add ( checked_model ) deps = model_dependencies ( checked_model , checked_models ) for dep in deps : if ( ( dep not in depends ) and ( dep not in new_to_check ) and ( dep not in checked_models ) ) : new_to_check . add ( dep ) depends . add ( dep ) return depends
returns a set of models this one depends on to be defined; things like onetoonefields as id .