[Django] #31282: RelatedManager.set(..) no longer accepts a list of PKs

12 views
Skip to first unread message

Django

unread,
Feb 17, 2020, 3:45:59 PM2/17/20
to django-...@googlegroups.com
#31282: RelatedManager.set(..) no longer accepts a list of PKs
-----------------------------------------+------------------------
Reporter: YLiLarry | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
It seems that I can no longer pass a list of pks to
RelatedManager.set(..).
Alternatively, passing a list of objects works as expected.

{{{
>>> p = Product.objects.all()[0]
>>> p
<Product: <Product pk=587 barcode=041390007019>>
>>> p.images.set([1])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 750, in set
self.add(*objs, bulk=bulk)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 656, in add
check_and_update_obj(obj)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 648, in
check_and_update_obj
raise TypeError("'%s' instance expected, got %r" % (
TypeError: 'ProductImage' instance expected, got 1
}}}

{{{
class Product(m.Model):
pass
}}}

{{{
class ProductImage(m.Model):
product = m.ForeignKey(
Product,
on_delete=m.CASCADE,
related_name='images',
)
}}}

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

Django

unread,
Feb 17, 2020, 3:49:01 PM2/17/20
to django-...@googlegroups.com
#31282: RelatedManager.set(..) no longer accepts a list of PKs
-------------------------------+--------------------------------------

Reporter: YLiLarry | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Old description:

New description:

{{{
>>> i = ProductImage.objects.get(pk=100)
>>> p.images.set([100])


Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 750, in set
self.add(*objs, bulk=bulk)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 656, in add
check_and_update_obj(obj)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 648, in
check_and_update_obj
raise TypeError("'%s' instance expected, got %r" % (

TypeError: 'ProductImage' instance expected, got 100

--

Comment (by YLiLarry):

{{{
>>> i = ProductImage.objects.get(pk=100)
>>> p.images.set([100])


Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 750, in set
self.add(*objs, bulk=bulk)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 656, in add
check_and_update_obj(obj)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 648, in
check_and_update_obj
raise TypeError("'%s' instance expected, got %r" % (

TypeError: 'ProductImage' instance expected, got 100
}}}

However, this works fine:
{{{
p.images.set([i])
}}}

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

Django

unread,
Feb 17, 2020, 3:51:43 PM2/17/20
to django-...@googlegroups.com
#31282: RelatedManager.set(..) no longer accepts a list of PKs
-------------------------------+--------------------------------------

Reporter: YLiLarry | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 3.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by YLiLarry:

Old description:

> {{{
> >>> i = ProductImage.objects.get(pk=100)
> >>> p.images.set([100])


> Traceback (most recent call last):
> File "<console>", line 1, in <module>
> File "/usr/local/lib/python3.8/dist-
> packages/django/db/models/fields/related_descriptors.py", line 750, in
> set
> self.add(*objs, bulk=bulk)
> File "/usr/local/lib/python3.8/dist-
> packages/django/db/models/fields/related_descriptors.py", line 656, in
> add
> check_and_update_obj(obj)
> File "/usr/local/lib/python3.8/dist-
> packages/django/db/models/fields/related_descriptors.py", line 648, in
> check_and_update_obj
> raise TypeError("'%s' instance expected, got %r" % (

> TypeError: 'ProductImage' instance expected, got 100

New description:

It seems that I can no longer pass a list of pks to
RelatedManager.set(..).
Alternatively, passing a list of objects works as expected.
{{{
>>> p = Product.objects.all()[0]
>>> p
<Product: <Product pk=587 barcode=041390007019>>
>>> p.images.set([1])
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 750, in set
self.add(*objs, bulk=bulk)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 656, in add
check_and_update_obj(obj)
File "/usr/local/lib/python3.8/dist-
packages/django/db/models/fields/related_descriptors.py", line 648, in
check_and_update_obj
raise TypeError("'%s' instance expected, got %r" % (
TypeError: 'ProductImage' instance expected, got 1

class Product(m.Model):
...

class ProductImage(m.Model):
product = m.ForeignKey(
Product,
on_delete=m.CASCADE,
related_name='images',
)
}}}

--

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

Django

unread,
Feb 18, 2020, 1:44:48 AM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
---------------------------------+------------------------------------
Reporter: Yu Li | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* cc: Tobias Kunze (added)
* component: Uncategorized => Documentation
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

`RelatedManager.set()` for reverse relations has never accepted a list of
IDs (unlike many-to-many).

There is a regression in docs introduced in
a44a21a22f20c1a710670676fcca798dd6bb5ac0

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

Django

unread,
Feb 18, 2020, 2:24:24 AM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: assigned

Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* owner: nobody => Carlton Gibson
* status: new => assigned


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

Django

unread,
Feb 18, 2020, 10:54:43 AM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: assigned
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/12466 PR]

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

Django

unread,
Feb 18, 2020, 1:54:46 PM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton@…>):

In [changeset:"a34cb5a6d408203f4fbdb364fc9768c026eda224" a34cb5a]:
{{{
#!CommitTicketReference repository=""
revision="a34cb5a6d408203f4fbdb364fc9768c026eda224"
Refs #31282 -- Clarified M2O add/remove/set with PK behaviour.

Improved error message for remove() and added tests.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31282#comment:7>

Django

unread,
Feb 18, 2020, 1:54:46 PM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"3bbf9a489afc689eff2f4a0b84af196aa1ef51e7" 3bbf9a48]:
{{{
#!CommitTicketReference repository=""
revision="3bbf9a489afc689eff2f4a0b84af196aa1ef51e7"
Fixed #31282 -- Corrected RelatedManager docs for using add/remove/set
with PKs.
}}}

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

Django

unread,
Feb 18, 2020, 1:58:56 PM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"611d1c114835310a0cd963e20f3e9782c34db847" 611d1c11]:
{{{
#!CommitTicketReference repository=""
revision="611d1c114835310a0cd963e20f3e9782c34db847"
[3.0.x] Fixed #31282 -- Corrected RelatedManager docs for using
add/remove/set with PKs.

Backport of 3bbf9a489afc689eff2f4a0b84af196aa1ef51e7 from master
}}}

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

Django

unread,
Feb 18, 2020, 1:59:31 PM2/18/20
to django-...@googlegroups.com
#31282: Docs for RelatedManager.set()/add()/remove() incorrectly states that the
field the relation points to is acceptable for one-to-many relations.
-------------------------------------+-------------------------------------
Reporter: Yu Li | Owner: Carlton
| Gibson
Type: Bug | Status: closed
Component: Documentation | Version: 3.0
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"7deb87c93e5f45ded3ccd1e6162bed6a30000af8" 7deb87c9]:
{{{
#!CommitTicketReference repository=""
revision="7deb87c93e5f45ded3ccd1e6162bed6a30000af8"
[2.2.x] Fixed #31282 -- Corrected RelatedManager docs for using
add/remove/set with PKs.

Backport of 3bbf9a489afc689eff2f4a0b84af196aa1ef51e7 from master
}}}

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

Reply all
Reply to author
Forward
0 new messages