#21144: Not possible to update foreign key by id for a queryset
-------------------------------------+-------------------------------------
     Reporter:  dakinsloss@…         |      Owner:  nobody
         Type:                       |     Status:  new
  Cleanup/optimization               |    Version:  1.5
    Component:  Database layer       |   Keywords:  update, related model,
  (models, ORM)                      |  performance
     Severity:  Normal               |  Has patch:  0
 Triage Stage:  Unreviewed           |      UI/UX:  0
Easy pickings:  0                    |
-------------------------------------+-------------------------------------
 Attempting to minimize queries when updating list of ModelA instances that
 have foreign keys to ModelB, and I know the ids of the new ModelB
 instances I want to use for the instances of ModelA (that I only have the
 id for, not the instance).

 Trying:
 ModelA.objects.filter(pk=id_of_a_instance).update(b_id=id_of_b_instance)
 which yields 'FieldNameDoesNotExist' because 'get_field_by_name' only has
 'b' as a field name for ModelA, not b_id (which is strange given that is
 what is actually stored).

 Then I try:
 ModelA.objects.filter(pk=id_of_a_instance).update(b=id_of_b_instance),
 which says it needs an instance of ModelB, not a number.

 Which means I will be stuck with an extra query:
 model_b_list = list(ModelB.objects.filter(id_in=[list of ids for a
 instances])
 and then have to iterate through the ModelA instances and assign the
 actual ModelB instances

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21144>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/063.7ad1272d35be91a3c717e366589628a7%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to