#33333: Models with a BinaryField fail to deepcopy
-------------------------------------+-------------------------------------
     Reporter:  Adam Zimmerman       |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     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
-------------------------------------+-------------------------------------

Comment (by Adam Zimmerman):

 For anyone who finds this ticket and wants to work around it before it's
 fixed in Django, here's the mixin I'm currently using:

 {{{#!python
 class ContainsBinaryField:
     """A mixin for model classes that contain BinaryField fields

     Django 3.2 has started using deepcopy() to isolate changes to data
 created in
     setUpTestData(). This seems to cause issues with models that have a
 BinaryField in
     them. This implements the __getstate__() method in a way that converts
 any
     memoryview data to bytes, which seems to fix the issue."""

     def __getstate__(self):
         state = super().__getstate__()
         for key, value in state.items():
             if isinstance(value, memoryview):
                 state[key] = bytes(value)
         return state
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33333#comment:1>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.84d66697ca35b55d84ec675180c4700c%40djangoproject.com.

Reply via email to