Best Python code snippet using autotest_python
model_logic.py
Source:model_logic.py
...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)254 def key_on_joined_table(self, join_to_query):255 """Get a non-null column on the table joined for the given query.256 This analyzes the join that would be produced if join_to_query were257 passed to join_custom_field.258 """259 relationship_type, field = self.determine_relationship(...
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!!