'Join to a related model to create a custom field in the given query.
This method is used to construct a custom field on the given query based
on a many-valued relationsip.  join_to_query should be a simple query
(no joins) on the related model which returns at most one related row
per instance of this model.
For many-to-one relationships, the joined table contains the matching
row from the related model it one is related, NULL otherwise.
For many-to-many relationships, the joined table contains the matching
row if it\'s related, NULL otherwise.'

def join_custom_field(self, query_set, join_to_query, alias, left_join=True):

    (relationship_type, field) = self.determine_relationship(join_to_query.model)
    if (relationship_type == self.MANY_TO_ONE):
        info = self._info_for_many_to_one_join(field, join_to_query, alias)
    elif (relationship_type == self.M2M_ON_RELATED_MODEL):
        info = self._info_for_many_to_many_join(m2m_field=field, join_to_query=join_to_query, alias=alias, m2m_is_on_this_model=False)
    elif (relationship_type == self.M2M_ON_THIS_MODEL):
        info = self._info_for_many_to_many_join(m2m_field=field, join_to_query=join_to_query, alias=alias, m2m_is_on_this_model=True)
    return self.add_join(query_set, info['rhs_table'], info['rhs_column'], join_from_key=info['lhs_column'], join_condition=info['where_clause'], join_condition_values=info['values'], alias=alias, force_left_join=left_join)
