(db_alias)
A coroutine that is sent object dictionaries and loads them into the
    database identified by ``db_alias``. When it is terminated with
    ``None``, it yields a LoadStat object.
A coroutine that is sent object dictionaries and loads them into the
    database identified by ``db_alias``. When it is terminated with
    ``None``, it yields a LoadStat object.
A coroutine that is sent object dictionaries and loads them into the database identified by db_alias . When it is terminated with None it yields a LoadStat object .
def load_data_for_db(db_alias):
    """
    A coroutine that is sent object dictionaries and loads them into the
    database identified by ``db_alias``. When it is terminated with
    ``None``, it yields a LoadStat object.
    """
    model_counter = Counter()
    with transaction.atomic(using=db_alias), \
         constraint_checks_deferred(db_alias):
        while True:
            obj_dict = yield
            if obj_dict is None:
                break
            for obj in PythonDeserializer([obj_dict], using=db_alias):
                Model = type(obj.object)
                if not router.allow_migrate_model(db_alias, Model):
                    continue
                model_counter.update([Model])
                try:
                    # Force insert here to prevent Django from attempting to do an update.
                    # We want to ensure that if there is already data in the DB that we don't
                    # save over it and rather error out.
                    obj.save(using=db_alias, force_insert=True)
                except DatabaseError as err:
                    logger.exception("Error saving data")
                    m = Model._meta
                    key = f"pk={obj.object.pk}"
                    if hasattr(obj.object, "natural_key"):
                        key = f"key={obj.object.natural_key()}"
                    raise type(err)(
                        f'Could not load {m.app_label}.{m.object_name}'
                        f'({key}) in DB {db_alias!r}'
                    ) from err
    print(f'Loading DB {db_alias!r} complete')
    yield LoadStat(db_alias, model_counter)
def load_data_for_db ( db_alias ) : model_counter = Counter ( ) with transaction . atomic ( using = db_alias ) , constraint_checks_deferred ( db_alias ) : while True : obj_dict = yield if obj_dict is None : break for obj in PythonDeserializer ( [ obj_dict ] , using = db_alias ) : Model = type ( obj . object ) if not router . allow_migrate_model ( db_alias , Model ) : continue model_counter . update ( [ Model ] ) try : # Force insert here to prevent Django from attempting to do an update. # We want to ensure that if there is already data in the DB that we don't # save over it and rather error out. obj . save ( using = db_alias , force_insert = True ) except DatabaseError as err : logger . exception ( "Error saving data" ) m = Model . _meta key = f"pk={obj.object.pk}" if hasattr ( obj . object , "natural_key" ) : key = f"key={obj.object.natural_key()}" raise type ( err ) ( f'Could not load {m.app_label}.{m.object_name}' f'({key}) in DB {db_alias!r}' ) from err print ( f'Loading DB {db_alias!r} complete' ) yield LoadStat ( db_alias , model_counter )