Best Python code snippet using autotest_python
model_logic.py
Source:model_logic.py  
...81        def combine(self, rhs, connector):82            super(ExtendedManager.CustomQuery, self).combine(rhs, connector)83            if hasattr(rhs, '_custom_joins'):84                self._custom_joins.extend(rhs._custom_joins)85        def add_custom_join(self, table, condition, join_type,86                            condition_values=(), alias=None):87            if alias is None:88                alias = table89            join_dict = dict(table=table,90                             condition=condition,91                             condition_values=condition_values,92                             join_type=join_type,93                             alias=alias)94            self._custom_joins.append(join_dict)95        @classmethod96        def convert_query(self, query_set):97            """98            Convert the query set's "query" attribute to a CustomQuery.99            """100            # Make a copy of the query set101            query_set = query_set.all()102            query_set.query = query_set.query.clone(103                klass=ExtendedManager.CustomQuery,104                _custom_joins=[])105            return query_set106    class _WhereClause(object):107        """Object allowing us to inject arbitrary SQL into Django queries.108        By using this instead of extra(where=...), we can still freely combine109        queries with & and |.110        """111        def __init__(self, clause, values=()):112            self._clause = clause113            self._values = values114        def as_sql(self, qn=None, connection=None):115            return self._clause, self._values116        def relabel_aliases(self, change_map):117            return118    def add_join(self, query_set, join_table, join_key, join_condition='',119                 join_condition_values=(), join_from_key=None, alias=None,120                 suffix='', exclude=False, force_left_join=False):121        """Add a join to query_set.122        Join looks like this:123                (INNER|LEFT) JOIN <join_table> AS <alias>124                    ON (<this table>.<join_from_key> = <join_table>.<join_key>125                        and <join_condition>)126        :param join_table table to join to127        :param join_key field referencing back to this model to use for the join128        :param join_condition extra condition for the ON clause of the join129        :param join_condition_values values to substitute into join_condition130        :param join_from_key column on this model to join from.131        :param alias alias to use for for join132        :param suffix suffix to add to join_table for the join alias, if no133                alias is provided134        :param exclude if true, exclude rows that match this join (will use a135        LEFT OUTER JOIN and an appropriate WHERE condition)136        :param force_left_join - if true, a LEFT OUTER JOIN will be used137        instead of an INNER JOIN regardless of other options138        """139        join_from_table = query_set.model._meta.db_table140        if join_from_key is None:141            join_from_key = self.model._meta.pk.name142        if alias is None:143            alias = join_table + suffix144        full_join_key = _quote_name(alias) + '.' + _quote_name(join_key)145        full_join_condition = '%s = %s.%s' % (full_join_key,146                                              _quote_name(join_from_table),147                                              _quote_name(join_from_key))148        if join_condition:149            full_join_condition += ' AND (' + join_condition + ')'150        if exclude or force_left_join:151            join_type = query_set.query.LOUTER152        else:153            join_type = query_set.query.INNER154        query_set = self.CustomQuery.convert_query(query_set)155        query_set.query.add_custom_join(join_table,156                                        full_join_condition,157                                        join_type,158                                        condition_values=join_condition_values,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        """...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!!
