Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31282 closed Bug (fixed)

Docs for RelatedManager.set()/add()/remove() incorrectly states that the field the relation points to is acceptable for one-to-many relations.

Reported by: Yu Li Owned by: Carlton Gibson
Component: Documentation Version: 3.0
Severity: Release blocker Keywords:
Cc: Tobias Kunze Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Yu Li)

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',
    )

Change History (9)

comment:1 by Yu Li, 5 years ago

Description: modified (diff)
>>> 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])

comment:2 by Yu Li, 5 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 5 years ago

Cc: Tobias Kunze added
Component: UncategorizedDocumentation
Severity: NormalRelease blocker
Summary: RelatedManager.set(..) no longer accepts a list of PKsDocs for RelatedManager.set()/add()/remove() incorrectly states that the field the relation points to is acceptable for one-to-many relations.
Triage Stage: UnreviewedAccepted

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

Version 0, edited 5 years ago by Mariusz Felisiak (next)

comment:4 by Carlton Gibson, 5 years ago

Owner: changed from nobody to Carlton Gibson
Status: newassigned

comment:5 by Mariusz Felisiak, 5 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:6 by Carlton Gibson <carlton@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 3bbf9a48:

Fixed #31282 -- Corrected RelatedManager docs for using add/remove/set with PKs.

comment:7 by Carlton Gibson <carlton@…>, 5 years ago

In a34cb5a:

Refs #31282 -- Clarified M2O add/remove/set with PK behaviour.

Improved error message for remove() and added tests.

comment:8 by Carlton Gibson <carlton.gibson@…>, 5 years ago

In 611d1c11:

[3.0.x] Fixed #31282 -- Corrected RelatedManager docs for using add/remove/set with PKs.

Backport of 3bbf9a489afc689eff2f4a0b84af196aa1ef51e7 from master

comment:9 by Carlton Gibson <carlton.gibson@…>, 5 years ago

In 7deb87c9:

[2.2.x] Fixed #31282 -- Corrected RelatedManager docs for using add/remove/set with PKs.

Backport of 3bbf9a489afc689eff2f4a0b84af196aa1ef51e7 from master

Note: See TracTickets for help on using tickets.
Back to Top