diff options
| author | Дилян Палаузов <Dilyan.Palauzov@db.com> | 2018-01-03 18:52:12 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-01-03 20:12:23 -0500 |
| commit | d7b2aa24f75434c2ce50100cfef3586071e0747a (patch) | |
| tree | 9074eb7522888e744f948c52174f367a4281c200 /django/db | |
| parent | c2d0f8c084456b5073252a91eeb09ab3d7453b18 (diff) | |
Fixed #28982 -- Simplified code with and/or.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/mysql/schema.py | 4 | ||||
| -rw-r--r-- | django/db/backends/postgresql/creation.py | 4 | ||||
| -rw-r--r-- | django/db/backends/postgresql/operations.py | 8 | ||||
| -rw-r--r-- | django/db/models/aggregates.py | 6 | ||||
| -rw-r--r-- | django/db/models/base.py | 11 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 3 | ||||
| -rw-r--r-- | django/db/models/fields/files.py | 4 | ||||
| -rw-r--r-- | django/db/models/lookups.py | 4 | ||||
| -rw-r--r-- | django/db/models/manager.py | 3 | ||||
| -rw-r--r-- | django/db/models/sql/compiler.py | 15 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 18 |
11 files changed, 29 insertions, 51 deletions
diff --git a/django/db/backends/mysql/schema.py b/django/db/backends/mysql/schema.py index 24abdaf611..997fef1c0c 100644 --- a/django/db/backends/mysql/schema.py +++ b/django/db/backends/mysql/schema.py @@ -61,9 +61,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): field.get_internal_type() == 'ForeignKey' and field.db_constraint): return False - if self._is_limited_data_type(field): - return False - return create_index + return not self._is_limited_data_type(field) and create_index def _delete_composed_index(self, model, fields, *args): """ diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py index a93bdbb4a1..0169fc5c1f 100644 --- a/django/db/backends/postgresql/creation.py +++ b/django/db/backends/postgresql/creation.py @@ -16,9 +16,7 @@ class DatabaseCreation(BaseDatabaseCreation): suffix += " ENCODING '{}'".format(encoding) if template: suffix += " TEMPLATE {}".format(self._quote_name(template)) - if suffix: - suffix = "WITH" + suffix - return suffix + return suffix and "WITH" + suffix def sql_table_creation_suffix(self): test_settings = self.connection.settings_dict['TEST'] diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 06a46f4303..3b71cd4f2c 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -131,11 +131,9 @@ class DatabaseOperations(BaseDatabaseOperations): sql = [] for sequence_info in sequences: table_name = sequence_info['table'] - column_name = sequence_info['column'] - if not column_name: - # This will be the case if it's an m2m using an autogenerated - # intermediate table (see BaseDatabaseIntrospection.sequence_list) - column_name = 'id' + # 'id' will be the case if it's an m2m using an autogenerated + # intermediate table (see BaseDatabaseIntrospection.sequence_list). + column_name = sequence_info['column'] or 'id' sql.append("%s setval(pg_get_serial_sequence('%s','%s'), 1, false);" % ( style.SQL_KEYWORD('SELECT'), style.SQL_TABLE(self.quote_name(table_name)), diff --git a/django/db/models/aggregates.py b/django/db/models/aggregates.py index c7284359d4..8b67151663 100644 --- a/django/db/models/aggregates.py +++ b/django/db/models/aggregates.py @@ -31,15 +31,13 @@ class Aggregate(Func): return source_expressions def set_source_expressions(self, exprs): - if self.filter: - self.filter = exprs.pop() + self.filter = self.filter and exprs.pop() return super().set_source_expressions(exprs) def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False): # Aggregates are not allowed in UPDATE queries, so ignore for_save c = super().resolve_expression(query, allow_joins, reuse, summarize) - if c.filter: - c.filter = c.filter.resolve_expression(query, allow_joins, reuse, summarize) + c.filter = c.filter and c.filter.resolve_expression(query, allow_joins, reuse, summarize) if not summarize: # Call Aggregate.get_source_expressions() to avoid # returning self.filter and including that in this loop. diff --git a/django/db/models/base.py b/django/db/models/base.py index 972da7bb06..f88690f225 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -549,8 +549,7 @@ class Model(metaclass=ModelBase): self.__dict__.update(state) def _get_pk_val(self, meta=None): - if not meta: - meta = self._meta + meta = meta or self._meta return getattr(self, meta.pk.attname) def _set_pk_val(self, value): @@ -852,7 +851,8 @@ class Model(metaclass=ModelBase): # exists. return update_fields is not None or filtered.exists() if self._meta.select_on_save and not forced_update: - if filtered.exists(): + return ( + filtered.exists() and # It may happen that the object is deleted from the DB right after # this check, causing the subsequent UPDATE to return zero matching # rows. The same result can occur in some rare cases when the @@ -860,9 +860,8 @@ class Model(metaclass=ModelBase): # successfully (a row is matched and updated). In order to # distinguish these two cases, the object's existence in the # database is again checked for if the UPDATE query returns 0. - return filtered._update(values) > 0 or filtered.exists() - else: - return False + (filtered._update(values) > 0 or filtered.exists()) + ) return filtered._update(values) > 0 def _do_insert(self, manager, using, fields, update_pk, raw): diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 6ddd8f033a..da3fca1213 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -694,8 +694,7 @@ class Field(RegisterLookupMixin): return self._db_tablespace or settings.DEFAULT_INDEX_TABLESPACE def set_attributes_from_name(self, name): - if not self.name: - self.name = name + self.name = self.name or name self.attname, self.column = self.get_attname_column() self.concrete = self.column is not None if self.verbose_name is None and self.name: diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 683a34ecd9..2d1d4b5e5f 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -314,9 +314,7 @@ class FileField(Field): if data is not None: # This value will be converted to str and stored in the # database, so leaving False as-is is not acceptable. - if not data: - data = '' - setattr(instance, self.name, data) + setattr(instance, self.name, data or '') def formfield(self, **kwargs): return super().formfield(**{ diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py index a9abd82cd9..7b01d06863 100644 --- a/django/db/models/lookups.py +++ b/django/db/models/lookups.py @@ -177,9 +177,7 @@ class FieldGetDbPrepValueMixin: def get_db_prep_lookup(self, value, connection): # For relational fields, use the output_field of the 'field' attribute. field = getattr(self.lhs.output_field, 'field', None) - get_db_prep_value = getattr(field, 'get_db_prep_value', None) - if not get_db_prep_value: - get_db_prep_value = self.lhs.output_field.get_db_prep_value + get_db_prep_value = getattr(field, 'get_db_prep_value', None) or self.lhs.output_field.get_db_prep_value return ( '%s', [get_db_prep_value(v, connection, prepared=True) for v in value] diff --git a/django/db/models/manager.py b/django/db/models/manager.py index 5f897cd3ca..e7a956cc51 100644 --- a/django/db/models/manager.py +++ b/django/db/models/manager.py @@ -107,8 +107,7 @@ class BaseManager: }) def contribute_to_class(self, model, name): - if not self.name: - self.name = name + self.name = self.name or name self.model = model setattr(model, name, ManagerDescriptor(self)) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index ddbcb0eb9a..a65c6e2596 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -521,8 +521,7 @@ class SQLCompiler: if grouping: if distinct_fields: raise NotImplementedError('annotate() + distinct(fields) is not implemented.') - if not order_by: - order_by = self.connection.ops.force_no_ordering() + order_by = order_by or self.connection.ops.force_no_ordering() result.append('GROUP BY %s' % ', '.join(grouping)) if having: @@ -588,8 +587,7 @@ class SQLCompiler: if opts is None: opts = self.query.get_meta() only_load = self.deferred_to_columns() - if not start_alias: - start_alias = self.query.get_initial_alias() + start_alias = start_alias or self.query.get_initial_alias() # The 'seen_models' is used to optimize checking the needed parent # alias for a given field. This also includes None -> start_alias to # be used by local fields. @@ -657,8 +655,7 @@ class SQLCompiler: # of the field is specified. if field.is_relation and opts.ordering and getattr(field, 'attname', None) != name: # Firstly, avoid infinite loops. - if not already_seen: - already_seen = set() + already_seen = already_seen or set() join_tuple = tuple(getattr(self.query.alias_map[j], 'join_cols', None) for j in joins) if join_tuple in already_seen: raise FieldError('Infinite loop caused by ordering.') @@ -680,8 +677,7 @@ class SQLCompiler: same input, as the prefixes of get_ordering() and get_distinct() must match. Executing SQL where this is not true is an error. """ - if not alias: - alias = self.query.get_initial_alias() + alias = alias or self.query.get_initial_alias() field, targets, opts, joins, path = self.query.setup_joins( pieces, opts, alias) alias = joins[-1] @@ -1037,8 +1033,7 @@ class SQLCompiler: is needed, as the filters describe an empty set. In that case, None is returned, to avoid any unnecessary database interaction. """ - if not result_type: - result_type = NO_RESULTS + result_type = result_type or NO_RESULTS try: sql, params = self.as_sql() if not sql: diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index bfee2256e6..e657594c86 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -805,9 +805,9 @@ class Query: if isinstance(self.group_by, tuple): self.group_by = tuple([col.relabeled_clone(change_map) for col in self.group_by]) self.select = tuple([col.relabeled_clone(change_map) for col in self.select]) - if self._annotations: - self._annotations = OrderedDict( - (key, col.relabeled_clone(change_map)) for key, col in self._annotations.items()) + self._annotations = self._annotations and OrderedDict( + (key, col.relabeled_clone(change_map)) for key, col in self._annotations.items() + ) # 2. Rename the alias in the internal table/alias datastructures. for old_alias, new_alias in change_map.items(): @@ -1061,9 +1061,7 @@ class Query: and get_transform(). """ # __exact is the default lookup if one isn't given. - if not lookups: - lookups = ['exact'] - + lookups = lookups or ['exact'] for name in lookups[:-1]: lhs = self.try_transform(lhs, name) # First try get_lookup() so that the lookup takes precedence if the lhs @@ -2050,10 +2048,10 @@ class Query: # used. The proper fix would be to defer all decisions where # is_nullable() is needed to the compiler stage, but that is not easy # to do currently. - if connections[DEFAULT_DB_ALIAS].features.interprets_empty_strings_as_nulls and field.empty_strings_allowed: - return True - else: - return field.null + return ( + connections[DEFAULT_DB_ALIAS].features.interprets_empty_strings_as_nulls and + field.empty_strings_allowed + ) or field.null def get_order_dir(field, default='ASC'): |
