#33582: loaddata fails on non-default database with one-to-many relationship 
that
uses natural keys
--------------------------------------+------------------------------------
     Reporter:  François Granade      |                    Owner:  nobody
         Type:  Bug                   |                   Status:  new
    Component:  Core (Serialization)  |                  Version:  4.0
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  0                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  0
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

 * stage:  Unreviewed => Accepted


Comment:

 Thanks for this report. I was able to fix the issue by specifying `db`
 before checking `natural_key()`:
 {{{#!diff
 diff --git a/django/core/serializers/base.py
 b/django/core/serializers/base.py
 index da85cb4b92..517d2cad85 100644
 --- a/django/core/serializers/base.py
 +++ b/django/core/serializers/base.py
 @@ -336,7 +336,9 @@ def build_instance(Model, data, db):
          and hasattr(default_manager, "get_by_natural_key")
          and hasattr(Model, "natural_key")
      ):
 -        natural_key = Model(**data).natural_key()
 +        obj = Model(**data)
 +        obj._state.db = db
 +        natural_key = obj.natural_key()
          try:
              data[Model._meta.pk.attname] = Model._meta.pk.to_python(
 default_manager.db_manager(db).get_by_natural_key(*natural_key).pk
 }}}

 What do you think? Would you like to prepare a patch? (a regression test
 is required).

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33582#comment:3>
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/0107017f9ba4ae2a-aae786ae-76c7-480c-b4ab-67f71e760bc4-000000%40eu-central-1.amazonses.com.

Reply via email to