diff --git a/django/core/db/__init__.py b/django/core/db/__init__.py index d59f44bce3..c4f06e4c6e 100644 --- a/django/core/db/__init__.py +++ b/django/core/db/__init__.py @@ -38,6 +38,7 @@ get_last_insert_id = dbmod.get_last_insert_id get_date_extract_sql = dbmod.get_date_extract_sql get_date_trunc_sql = dbmod.get_date_trunc_sql get_limit_offset_sql = dbmod.get_limit_offset_sql +get_random_function_sql = dbmod.get_random_function_sql get_table_list = dbmod.get_table_list get_relations = dbmod.get_relations OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING diff --git a/django/core/db/backends/mysql.py b/django/core/db/backends/mysql.py index f65398b3b6..2193c6fbaa 100644 --- a/django/core/db/backends/mysql.py +++ b/django/core/db/backends/mysql.py @@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None): sql += "%s," % offset return sql + str(limit) +def get_random_function_sql(): + return "RAND()" + def get_table_list(cursor): "Returns a list of table names in the current database." cursor.execute("SHOW TABLES") diff --git a/django/core/db/backends/postgresql.py b/django/core/db/backends/postgresql.py index 0afeec6f30..150bc3f888 100644 --- a/django/core/db/backends/postgresql.py +++ b/django/core/db/backends/postgresql.py @@ -77,6 +77,9 @@ def get_limit_offset_sql(limit, offset=None): sql += " OFFSET %s" % offset return sql +def get_random_function_sql(): + return "RANDOM()" + def get_table_list(cursor): "Returns a list of table names in the current database." cursor.execute(""" diff --git a/django/core/db/backends/sqlite3.py b/django/core/db/backends/sqlite3.py index eef596eff0..fd44341400 100644 --- a/django/core/db/backends/sqlite3.py +++ b/django/core/db/backends/sqlite3.py @@ -103,6 +103,9 @@ def get_limit_offset_sql(limit, offset=None): sql += " OFFSET %s" % offset return sql +def get_random_function_sql(): + return "RANDOM()" + def _sqlite_date_trunc(lookup_type, dt): try: dt = typecasts.typecast_timestamp(dt) diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index a35498d87a..bc225f584f 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -62,7 +62,7 @@ def orderlist2sql(order_list, opts, prefix=''): if f.startswith('-'): output.append('%s%s DESC' % (prefix, orderfield2column(f[1:], opts))) elif f == '?': - output.append('RANDOM()') + output.append(db.get_random_function_sql()) else: output.append('%s%s ASC' % (prefix, orderfield2column(f, opts))) return ', '.join(output) @@ -1319,7 +1319,7 @@ def function_get_sql_clause(opts, **kwargs): order_by = [] for f in handle_legacy_orderlist(kwargs.get('order_by', opts.ordering)): if f == '?': # Special case. - order_by.append('RANDOM()') + order_by.append(db.get_random_function_sql()) else: # Use the database table as a column prefix if it wasn't given, # and if the requested column isn't a custom SELECT.