#33312: Errors when trying to clone an object from a deferred queryset
-------------------------------------+-------------------------------------
               Reporter:  Adam       |          Owner:  nobody
  Sołtysik                           |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 As per [https://docs.djangoproject.com/en/3.2/topics/db/queries/#copying-
 model-instances documentation], it's possible to clone objects by setting
 `pk = None`.

 However, if the object to clone comes from a deferred queryset (after
 calling `defer` or `only`), trying to save the copy raises some hard to
 decipher exceptions.

 {{{
 class DeferCloneTest(TestCase):
     @classmethod
     def setUpTestData(cls):
         SimpleItem.objects.create(name="test", value=42)

     def _get_item(self):
         item = SimpleItem.objects.defer('value').first()  # same with
 `only` instead of `defer`
         item.pk = None
         item._state.adding = True  # doesn't seem to change anything
         return item

     def test_save(self):
         self._get_item().save()  # ValueError: Cannot force an update in
 save() with no primary key.

     def test_save_force_insert(self):
         self._get_item().save(force_insert=True)  #
 SimpleItem.DoesNotExist

     def test_bulk_create(self):
         SimpleItem.objects.bulk_create([self._get_item()])  #
 SimpleItem.DoesNotExist
 }}}

 Possibly related: #27419, #28019.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.e66392a705d7e9175ab19ed220a350be%40djangoproject.com.

Reply via email to