#27128: A method model.objects.get(pk=obj.pk) returns many objects, but in a
database is one.
-------------------------------------+-------------------------------------
Reporter: setivolkylany | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution: needsinfo
Keywords: GenericRelation, | Triage Stage:
Testing, Models | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by setivolkylany):
Replying to [comment:6 timgraham]:
> If the queryset isn't ordered, each time you index
`get_user_model().objects.all()`, there's no guarantee that the results
will be in the same order. Try indexing
`get_user_model().objects.order_by('pk')` instead.
Your method given me properly results
{{{
users = get_user_model().objects.order_by('pk')
print(users)
users[0], users[1], users[2], users[3], users[4], users[5]
}}}
[<User: [email protected]>, <User: greencarl@francis-
brock.com>, <User: [email protected]>, <User: [email protected]>,
<User: [email protected]>, <User: [email protected]>, <User:
[email protected]>, <User: [email protected]>, <User:
[email protected]>]
(<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>)
My default code still return not properly results
{{{
users = get_user_model().objects.all()
print(users)
users[0], users[1], users[2], users[3], users[4], users[5]
}}}
[<User: [email protected]>, <User: [email protected]>, <User:
[email protected]>, <User: [email protected]>, <User: greencarl@francis-
brock.com>, <User: [email protected]>, <User: [email protected]>,
<User: [email protected]>, <User: [email protected]>]
(<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>,
<User: [email protected]>)
But it not resolve my problem, because my user`s models has default
sorting by Last_login (see SQL)
Sorting for the model if I override it
{{{
print(get_user_model().objects.only('pk').order_by('pk').query)
}}}
SELECT "user"."id" FROM "user" ORDER BY "user"."id" ASC
Sorting for the model by default is by last_login of users
{{{
print(get_user_model().objects.only('pk').query)
}}}
SELECT "user"."id" FROM "user" ORDER BY "user"."last_login" DESC
--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/071.8c945485fba56abb5c29d133231ddce4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.