MCPcopy
hub / github.com/django/django / _save_parents

Method _save_parents

django/db/models/base.py:1014–1059  ·  view source on GitHub ↗

Save all the parents of cls using values from self.

(
        self, cls, using, update_fields, force_insert, updated_parents=None
    )

Source from the content-addressed store, hash-verified

1012 save_base.alters_data = True
1013
1014 def _save_parents(
1015 self, cls, using, update_fields, force_insert, updated_parents=None
1016 ):
1017 """Save all the parents of cls using values from self."""
1018 meta = cls._meta
1019 inserted = False
1020 if updated_parents is None:
1021 updated_parents = {}
1022 for parent, field in meta.parents.items():
1023 # Make sure the link fields are synced between parent and self.
1024 if (
1025 field
1026 and getattr(self, parent._meta.pk.attname) is None
1027 and getattr(self, field.attname) is not None
1028 ):
1029 setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
1030 if (parent_updated := updated_parents.get(parent)) is None:
1031 parent_inserted = self._save_parents(
1032 cls=parent,
1033 using=using,
1034 update_fields=update_fields,
1035 force_insert=force_insert,
1036 updated_parents=updated_parents,
1037 )
1038 updated = self._save_table(
1039 cls=parent,
1040 using=using,
1041 update_fields=update_fields,
1042 force_insert=parent_inserted or issubclass(parent, force_insert),
1043 )
1044 if not updated:
1045 inserted = True
1046 updated_parents[parent] = updated
1047 elif not parent_updated:
1048 inserted = True
1049 # Set the parent's PK value to self.
1050 if field:
1051 setattr(self, field.attname, self._get_pk_val(parent._meta))
1052 # Since we didn't have an instance of the parent handy set
1053 # attname directly, bypassing the descriptor. Invalidate
1054 # the related object cache, in case it's been accidentally
1055 # populated. A fresh instance will be re-built from the
1056 # database if necessary.
1057 if field.is_cached(self):
1058 field.delete_cached_value(self)
1059 return inserted
1060
1061 def _save_table(
1062 self,

Callers 1

save_baseMethod · 0.95

Calls 6

_save_tableMethod · 0.95
_get_pk_valMethod · 0.95
delete_cached_valueMethod · 0.80
itemsMethod · 0.45
getMethod · 0.45
is_cachedMethod · 0.45

Tested by

no test coverage detected