MCPcopy
hub / github.com/django/django / _save_table

Method _save_table

django/db/models/base.py:1061–1196  ·  view source on GitHub ↗

Do the heavy-lifting involved in saving. Update or insert the data for a single table.

(
        self,
        raw=False,
        cls=None,
        force_insert=False,
        force_update=False,
        using=None,
        update_fields=None,
    )

Source from the content-addressed store, hash-verified

1059 return inserted
1060
1061 def _save_table(
1062 self,
1063 raw=False,
1064 cls=None,
1065 force_insert=False,
1066 force_update=False,
1067 using=None,
1068 update_fields=None,
1069 ):
1070 """
1071 Do the heavy-lifting involved in saving. Update or insert the data
1072 for a single table.
1073 """
1074 meta = cls._meta
1075 pk_fields = meta.pk_fields
1076 non_pks_non_generated = [
1077 f
1078 for f in meta.local_concrete_fields
1079 if f not in pk_fields and not f.generated
1080 ]
1081
1082 if update_fields:
1083 non_pks_non_generated = [
1084 f
1085 for f in non_pks_non_generated
1086 if f.name in update_fields or f.attname in update_fields
1087 ]
1088
1089 if not self._is_pk_set(meta):
1090 pk_val = meta.pk.get_pk_value_on_save(self)
1091 setattr(self, meta.pk.attname, pk_val)
1092 pk_set = self._is_pk_set(meta)
1093 if not pk_set and (force_update or update_fields):
1094 raise ValueError("Cannot force an update in save() with no primary key.")
1095 updated = False
1096 # Skip an UPDATE when adding an instance and primary key has a default.
1097 if (
1098 not raw
1099 and not force_insert
1100 and not force_update
1101 and self._state.adding
1102 and all(f.has_default() or f.has_db_default() for f in meta.pk_fields)
1103 ):
1104 force_insert = True
1105 # If possible, try an UPDATE. If that doesn't update anything, do an
1106 # INSERT.
1107 if pk_set and not force_insert:
1108 base_qs = cls._base_manager.using(using)
1109 values = [
1110 (
1111 f,
1112 None,
1113 (getattr(self, f.attname) if raw else f.pre_save(self, False)),
1114 )
1115 for f in non_pks_non_generated
1116 ]
1117 forced_update = update_fields or force_update
1118 pk_val = self._get_pk_val(meta)

Callers 2

save_baseMethod · 0.95
_save_parentsMethod · 0.95

Calls 15

_is_pk_setMethod · 0.95
_get_pk_valMethod · 0.95
_do_updateMethod · 0.95
_do_insertMethod · 0.95
CoalesceClass · 0.90
ExpressionWrapperClass · 0.90
MaxClass · 0.90
ValueClass · 0.90
IntegerFieldClass · 0.90
has_defaultMethod · 0.80
has_db_defaultMethod · 0.80

Tested by

no test coverage detected