{{{
class Person(models.Model):
name = models.CharField(max_length=100)
country_code = models.CharField(max_length=3, blank=True, null=True)
class Meta:
db_table = 'persons'
}}}
I want to pick an entry randomly by using
{{{
Person.objects.all().order_by('?')
}}}
the resulting query has the following order by clause
{{{
... FROM [persons] ORDER BY RAND() ASC
}}}
This however does not give a randomized order of the table. Instead it
always returns the same entries in the same order(ordered by id).
The desired result can be achieved with
{{{
... ORDER BY NEWID()
}}}
I am aware that this also isn't an exactly optimal solution as stated
here: [https://dba.stackexchange.com/questions/955/what-is-the-best-way-
to-get-a-random-ordering].
For completeness: I observed this behaviour using a MSSQL14 Server
--
Ticket URL: <https://code.djangoproject.com/ticket/33531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => invalid
Comment:
Thanks for this report, however Django 3.0 is not supported anymore and
the backend for MSSQL is not built-in. You should report this issue on a
bug tracker for the 3rd database backend you're using.
--
Ticket URL: <https://code.djangoproject.com/ticket/33531#comment:1>