Changes between Initial Version and Version 1 of Ticket #31282


Ignore:
Timestamp:
Feb 17, 2020, 2:48:59 PM (5 years ago)
Author:
Yu Li
Comment:
>>> 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])

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #31282 – Description

    initial v1  
    1 It seems that I can no longer pass a list of pks to RelatedManager.set(..).
    2 Alternatively, passing a list of objects works as expected.
    3 
    41{{{
    5 >>> p = Product.objects.all()[0]
    6 >>> p
    7 <Product: <Product pk=587 barcode=041390007019>>
    8 >>> p.images.set([1])
     2>>> i = ProductImage.objects.get(pk=100)
     3>>> p.images.set([100])
    94Traceback (most recent call last):
    105  File "<console>", line 1, in <module>
     
    1510  File "/usr/local/lib/python3.8/dist-packages/django/db/models/fields/related_descriptors.py", line 648, in check_and_update_obj
    1611    raise TypeError("'%s' instance expected, got %r" % (
    17 TypeError: 'ProductImage' instance expected, got 1
    18 }}}
     12TypeError: 'ProductImage' instance expected, got 100
    1913
    20 {{{
    21 class Product(m.Model):
    22     pass
    23 }}}
    24 
    25 {{{
    26 class ProductImage(m.Model):
    27     product = m.ForeignKey(
    28         Product,
    29         on_delete=m.CASCADE,
    30         related_name='images',
    31     )
    32 }}}
    33 
Back to Top