code: def _cmp(cls, value1, value2):
          
          if isinstance(value1, Model) and isinstance(value2, Model):
              field_names = value1._meta.ordering
  
              # Assert that the ordering is the same between different models.
              if field_names != value2._meta.ordering:
                  valid_field_names = (set(cls._get_field_names(value1)) &
                                       set(cls._get_field_names(value2)))
                  raise FieldError(
                      "Ordering differs between models. Choices are: %s" %
                      ', '.join(valid_field_names))
  
              # By default, order by the pk.
              if not field_names:
                  field_names = ['pk']
  
              # TODO Figure out if we don't need to generate this comparator every
              # time.
              return cls._generate_comparator(field_names)(value1, value2)
  
          return cmp(value1, value2)
docs: Comparison method that takes into account Django's special rules when
          ordering by a field that is a model:
  
              1. Try following the default ordering on the related model.
              2. Order by the model's primary key, if there is no Meta.ordering.