Docs for RelatedManager.set()/add()/remove() incorrectly states that the field the relation points to is acceptable for one-to-many relations.
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)
Description: |
modified (diff)
|
Description: |
modified (diff)
|
Cc: |
Tobias Kunze added
|
Component: |
Uncategorized → Documentation
|
Severity: |
Normal → Release blocker
|
Summary: |
RelatedManager.set(..) no longer accepts a list of PKs → Docs for RelatedManager.set()/add()/remove() incorrectly states that the field the relation points to is acceptable for one-to-many relations.
|
Triage Stage: |
Unreviewed → Accepted
|
Owner: |
changed from nobody to Carlton Gibson
|
Status: |
new → assigned
|
Has patch: |
set
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
However, this works fine: