#31095: Related Manager set() should prepare values before checking for missing
elements.
-------------------------------------+-------------------------------------
     Reporter:  TZanke               |                    Owner:  nobody
         Type:                       |                   Status:  new
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  1                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by felixxm):

 * version:  1.11 => master
 * type:  Bug => Cleanup/optimization
 * easy:  0 => 1
 * stage:  Unreviewed => Accepted


Comment:

 We cannot raise an exception on strings because `set()` accepts the field
 the relation points, e.g. `CharField`. However we can optimize this with
 preparing values, i.e.
 {{{
 diff --git a/django/db/models/fields/related_descriptors.py
 b/django/db/models/fields/related_descriptors.py
 index a9445d5d10..9f82ca4e8c 100644
 --- a/django/db/models/fields/related_descriptors.py
 +++ b/django/db/models/fields/related_descriptors.py
 @@ -999,7 +999,7 @@ def create_forward_many_to_many_manager(superclass,
 rel, reverse):
                      for obj in objs:
                          fk_val = (
 self.target_field.get_foreign_related_value(obj)[0]
 -                            if isinstance(obj, self.model) else obj
 +                            if isinstance(obj, self.model) else
 self.target_field.get_prep_value(obj)
                          )
                          if fk_val in old_ids:
                              old_ids.remove(fk_val)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31095#comment:2>
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/064.fee167c5a37174524a060909676cb7dc%40djangoproject.com.

Reply via email to