[Django] #27128: A method model.objects.get(pk=obj.pk) returns many objects, but in a database is one.

50 views
Skip to first unread message

Django

unread,
Aug 26, 2016, 9:04:47 AM8/26/16
to django-...@googlegroups.com
#27128: A method model.objects.get(pk=obj.pk) returns many objects, but in a
database is one.
----------------------------+----------------------------------------------
Reporter: | Owner: nobody
setivolkylany |
Type: Bug | Status: new
Component: | Version: 1.9
Uncategorized |
Severity: Normal | Keywords: GenericRelation, Testing, Models
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+----------------------------------------------
Very strange behaviour of the Django ORM while testing (now I am using
pytest)

I have two apps "books" and "replies".
The app "books" has two models http://pastebin.com/gDMi7UpU
The app "replies" has one model http://pastebin.com/u9FabQQA

For create new objects I am using factories for replies -
http://pastebin.com/pGtiwS2m, for books and writers -
http://pastebin.com/NQ6AWxib

While testing methods of queryset of the model Book I got error, where it
should not be

A file querysets.py of the app "books" http://pastebin.com/FVnLLzxK
Tests for the file querysets.py of the app "books"
http://pastebin.com/rmpDm0Ae

For testing I am using the pytest with next configuration in a file
pytest.ini

{{{
[pytest]
addopts = -s --reuse-db --ff --maxfail=1
apps/books/tests/test_querysets.py::Tests
DJANGO_SETTINGS_MODULE=config.settings.development
}}}


Run test as next py.test

Traceback http://pastebin.com/Vv96XLzm

After many time googling and dipping in the Django`s code by the traceback
I decided it is bug in the Django

I think it is the problem related with that models the Book and the Reply
is related by help the field GenericRelation, but I don`t found facts for
it.

Sorry for my grammar mistakes, I was no originally an English

If need another codes from my project, please tell about it

--
Ticket URL: <https://code.djangoproject.com/ticket/27128>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 26, 2016, 9:13:00 AM8/26/16
to django-...@googlegroups.com
#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: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Resolution:
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
-------------------------------------+-------------------------------------
Changes (by setivolkylany):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

New description:

Traceback http://pastebin.com/Vv96XLzm

Update
Same error I had when tested a method "get_admin_url()" of the model Book
with next a testing code

{{{
def test_get_admin_url(self):
self.client.force_login(self.active_superuser)
response = self.client.get(self.book.get_admin_url())
self.assertEqual(response.status_code, 200)
}}}

Ending a traceback

{{{
if not num:
raise self.model.DoesNotExist(
"%s matching query does not exist." %
self.model._meta.object_name
)
raise self.model.MultipleObjectsReturned(
"get() returned more than one %s -- it returned %s!" %
> (self.model._meta.object_name, num)
)
E apps.books.models.MultipleObjectsReturned: get() returned more
than one Book -- it returned 3!
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:1>

Django

unread,
Aug 26, 2016, 9:13:34 AM8/26/16
to django-...@googlegroups.com
#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
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => needsinfo


Comment:

Yes, we need a minimal project (or a test case for Django's test suite, if
possible) without any third-party apps to rule out a bug there. Please
reopen if you can provide that -- and attach it to the ticket since
pastebin posts will expire soon.

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:2>

Django

unread,
Aug 26, 2016, 11:18:49 AM8/26/16
to django-...@googlegroups.com
#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:2 timgraham]:


> Yes, we need a minimal project (or a test case for Django's test suite,
if possible) without any third-party apps to rule out a bug there. Please
reopen if you can provide that -- and attach it to the ticket since
pastebin posts will expire soon.

Here two problems:
1. If is variant with minimal project, be taken into account that I using
the factory_boy for generate fake objects (it is third-party app)
2. What is "test case for Django's test suite"? How to made it to you?

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:3>

Django

unread,
Aug 26, 2016, 11:57:43 AM8/26/16
to django-...@googlegroups.com
#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 timgraham):

Yes, we need a project without `factory_boy` to ensure that the problem
isn't in that library.

You can read about [https://github.com/django/django/tree/master/tests
Django's test suite] in our
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/unit-tests/ contributing docs].

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:4>

Django

unread,
Aug 27, 2016, 7:33:33 AM8/27/16
to django-...@googlegroups.com
#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:4 timgraham]:


> Yes, we need a project without `factory_boy` to ensure that the problem
isn't in that library.
>
> You can read about [https://github.com/django/django/tree/master/tests
Django's test suite] in our
[https://docs.djangoproject.com/en/dev/internals/contributing/writing-code
/unit-tests/ contributing docs].

After many hours debug my code I found a next problem


{{{
(Pdb) get_user_model().objects.all()
[<User: deniser...@schmidt-clark.com>, <User: othompson@adams-
hess.com>, <User: salazark...@gonzalez-bennett.org>, <User:
kn...@hotmail.com>, <User: mayk...@mahoney.info>, <User:
robing...@long.com>, <User: michae...@hotmail.com>, <User:
cory...@gmail.com>]
(Pdb) get_user_model().objects.all()[0]
<User: deniser...@schmidt-clark.com>
(Pdb) get_user_model().objects.all()[1]
<User: deniser...@schmidt-clark.com>
(Pdb) get_user_model().objects.all()[2]
<User: deniser...@schmidt-clark.com>
(Pdb) get_user_model().objects.all()[3]
<User: kn...@hotmail.com>
(Pdb) get_user_model().objects.all()[4]
<User: mayk...@mahoney.info>
(Pdb) get_user_model().objects.all()[5]
<User: robing...@long.com>
}}}

Problem with a user`s model. It on index [0] [1] and [2] return the same
user. It leads to dublication in my replies.

If you know same problem, may be it fixed in the Django 1.10, if not know
- I will still debug.

I wait your response

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:5>

Django

unread,
Aug 27, 2016, 7:53:52 AM8/27/16
to django-...@googlegroups.com
#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 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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:6>

Django

unread,
Aug 27, 2016, 8:17:43 AM8/27/16
to django-...@googlegroups.com
#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: blackm...@gutierrez.com>, <User: greencarl@francis-
brock.com>, <User: ad...@admin.com>, <User: christ...@gmail.com>,
<User: jan...@gmail.com>, <User: sco...@gmail.com>, <User:
an...@hotmail.com>, <User: patri...@yahoo.com>, <User:
zfu...@jones.com>]
(<User: blackm...@gutierrez.com>,
<User: gree...@francis-brock.com>,
<User: ad...@admin.com>,
<User: christ...@gmail.com>,
<User: jan...@gmail.com>,
<User: sco...@gmail.com>)

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: ad...@admin.com>, <User: zfu...@jones.com>, <User:
an...@hotmail.com>, <User: sco...@gmail.com>, <User: greencarl@francis-
brock.com>, <User: patri...@yahoo.com>, <User: christ...@gmail.com>,
<User: blackm...@gutierrez.com>, <User: jan...@gmail.com>]
(<User: ad...@admin.com>,
<User: ad...@admin.com>,
<User: ad...@admin.com>,
<User: ad...@admin.com>,
<User: gree...@francis-brock.com>,
<User: patri...@yahoo.com>)

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

unread,
Aug 28, 2016, 3:11:18 PM8/28/16
to django-...@googlegroups.com
#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 shaib):

Replying to [comment:7 setivolkylany]:

Hi,

You are unlikely to get any more responses on this ticket -- it looks like
your original problem is not caused by a bug in Django, and now you have
started to ask about a different issue.

Please use the support channels (#django IRC channel, django-users mailing
list) to ask more questions.

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:8>

Django

unread,
Aug 30, 2016, 1:56:12 PM8/30/16
to django-...@googlegroups.com
#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:8 shaib]:


> Replying to [comment:7 setivolkylany]:
>
> Hi,
>
> You are unlikely to get any more responses on this ticket -- it looks
like your original problem is not caused by a bug in Django, and now you
have started to ask about a different issue.
>
> Please use the support channels (#django IRC channel, django-users
mailing list) to ask more questions.

I just made an assumption, but I just in case I am sorry about it, if this
caused offence in you

And about the the #django IRC channel the django-users mailing list much
help there can not be obtained

So, I am try to resolve my problems alone.

--
Ticket URL: <https://code.djangoproject.com/ticket/27128#comment:9>

Reply all
Reply to author
Forward
0 new messages