Best Python code snippet using autotest_python
model_logic.py
Source:model_logic.py  
...159                                        alias=alias)160        if exclude:161            query_set = query_set.extra(where=[full_join_key + ' IS NULL'])162        return query_set163    def _info_for_many_to_one_join(self, field, join_to_query, alias):164        """165        :param field: the ForeignKey field on the related model166        :param join_to_query: the query over the related model that we're167                joining to168        :param alias: alias of joined table169        """170        info = {}171        rhs_table = join_to_query.model._meta.db_table172        info['rhs_table'] = rhs_table173        info['rhs_column'] = field.column174        info['lhs_column'] = field.rel.get_related_field().column175        rhs_where = join_to_query.query.where176        rhs_where.relabel_aliases({rhs_table: alias})177        compiler = join_to_query.query.get_compiler(using=join_to_query.db)178        where_clause, values = rhs_where.as_sql(179            compiler.quote_name_unless_alias,180            compiler.connection)181        info['where_clause'] = where_clause182        info['values'] = values183        return info184    def _info_for_many_to_many_join(self, m2m_field, join_to_query, alias,185                                    m2m_is_on_this_model):186        """187        :param m2m_field: a Django field representing the M2M relationship.188                It uses a pivot table with the following structure:189                this model table <---> M2M pivot table <---> joined model table190        :param join_to_query: the query over the related model that we're191                joining to.192        :param alias: alias of joined table193        """194        if m2m_is_on_this_model:195            # referenced field on this model196            lhs_id_field = self.model._meta.pk197            # foreign key on the pivot table referencing lhs_id_field198            m2m_lhs_column = m2m_field.m2m_column_name()199            # foreign key on the pivot table referencing rhd_id_field200            m2m_rhs_column = m2m_field.m2m_reverse_name()201            # referenced field on related model202            rhs_id_field = m2m_field.rel.get_related_field()203        else:204            lhs_id_field = m2m_field.rel.get_related_field()205            m2m_lhs_column = m2m_field.m2m_reverse_name()206            m2m_rhs_column = m2m_field.m2m_column_name()207            rhs_id_field = join_to_query.model._meta.pk208        info = {}209        info['rhs_table'] = m2m_field.m2m_db_table()210        info['rhs_column'] = m2m_lhs_column211        info['lhs_column'] = lhs_id_field.column212        # select the ID of related models relevant to this join.  we can only do213        # a single join, so we need to gather this information up front and214        # include it in the join condition.215        rhs_ids = join_to_query.values_list(rhs_id_field.attname, flat=True)216        assert len(rhs_ids) == 1, ('Many-to-many custom field joins can only '217                                   'match a single related object.')218        rhs_id = rhs_ids[0]219        info['where_clause'] = '%s.%s = %s' % (_quote_name(alias),220                                               _quote_name(m2m_rhs_column),221                                               rhs_id)222        info['values'] = ()223        return info224    def join_custom_field(self, query_set, join_to_query, alias,225                          left_join=True):226        """Join to a related model to create a custom field in the given query.227        This method is used to construct a custom field on the given query based228        on a many-valued relationsip.  join_to_query should be a simple query229        (no joins) on the related model which returns at most one related row230        per instance of this model.231        For many-to-one relationships, the joined table contains the matching232        row from the related model it one is related, NULL otherwise.233        For many-to-many relationships, the joined table contains the matching234        row if it's related, NULL otherwise.235        """236        relationship_type, field = self.determine_relationship(237            join_to_query.model)238        if relationship_type == self.MANY_TO_ONE:239            info = self._info_for_many_to_one_join(field, join_to_query, alias)240        elif relationship_type == self.M2M_ON_RELATED_MODEL:241            info = self._info_for_many_to_many_join(242                m2m_field=field, join_to_query=join_to_query, alias=alias,243                m2m_is_on_this_model=False)244        elif relationship_type == self.M2M_ON_THIS_MODEL:245            info = self._info_for_many_to_many_join(246                m2m_field=field, join_to_query=join_to_query, alias=alias,247                m2m_is_on_this_model=True)248        return self.add_join(query_set, info['rhs_table'], info['rhs_column'],249                             join_from_key=info['lhs_column'],250                             join_condition=info['where_clause'],251                             join_condition_values=info['values'],252                             alias=alias,253                             force_left_join=left_join)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
