'Return a list of distinct values for a given field.
:param field: the field to select distinct values from
.. note:: This is a command and won\'t take ordering or limit into
account.
.. versionadded:: 0.4
.. versionchanged:: 0.5 - Fixed handling references
.. versionchanged:: 0.6 - Improved db_field refrence handling'

def distinct(self, field):

    queryset = self.clone()
    try:
        field = self._fields_to_dbfields([field]).pop()
    except LookUpError:
        pass
    distinct = self._dereference(queryset._cursor.distinct(field), 1, name=field, instance=self._document)
    doc_field = self._document._fields.get(field.split('.', 1)[0])
    instance = None
    EmbeddedDocumentField = _import_class('EmbeddedDocumentField')
    ListField = _import_class('ListField')
    GenericEmbeddedDocumentField = _import_class('GenericEmbeddedDocumentField')
    if isinstance(doc_field, ListField):
        doc_field = getattr(doc_field, 'field', doc_field)
    if isinstance(doc_field, (EmbeddedDocumentField, GenericEmbeddedDocumentField)):
        instance = getattr(doc_field, 'document_type', None)
    if ('.' in field):
        for field_part in field.split('.')[1:]:
            if (instance and isinstance(doc_field, (EmbeddedDocumentField, GenericEmbeddedDocumentField))):
                doc_field = instance
            doc_field = getattr(doc_field, field_part, doc_field)
            if isinstance(doc_field, ListField):
                doc_field = getattr(doc_field, 'field', doc_field)
            if isinstance(doc_field, (EmbeddedDocumentField, GenericEmbeddedDocumentField)):
                instance = getattr(doc_field, 'document_type', None)
    if (instance and isinstance(doc_field, (EmbeddedDocumentField, GenericEmbeddedDocumentField))):
        distinct = [instance(**doc) for doc in distinct]
    return distinct
