MCPcopy
hub / github.com/django/django / add_fields

Method add_fields

django/forms/models.py:1183–1221  ·  view source on GitHub ↗
(self, form, index)

Source from the content-addressed store, hash-verified

1181 return super().save_new(form, commit=commit)
1182
1183 def add_fields(self, form, index):
1184 super().add_fields(form, index)
1185 if self._pk_field == self.fk:
1186 name = self._pk_field.name
1187 kwargs = {"pk_field": True}
1188 else:
1189 # The foreign key field might not be on the form, so we poke at the
1190 # Model field to get the label, since we need that for error
1191 # messages.
1192 name = self.fk.name
1193 kwargs = {
1194 "label": getattr(
1195 form.fields.get(name), "label", capfirst(self.fk.verbose_name)
1196 )
1197 }
1198
1199 # The InlineForeignKeyField assumes that the foreign key relation is
1200 # based on the parent model's pk. If this isn't the case, set to_field
1201 # to correctly resolve the initial form value.
1202 if self.fk.remote_field.field_name != self.fk.remote_field.model._meta.pk.name:
1203 kwargs["to_field"] = self.fk.remote_field.field_name
1204
1205 # If we're adding a new object, ignore a parent's auto-generated key
1206 # as it will be regenerated on the save request.
1207 if self.instance._state.adding:
1208 if kwargs.get("to_field") is not None:
1209 to_field = self.instance._meta.get_field(kwargs["to_field"])
1210 else:
1211 to_field = self.instance._meta.pk
1212
1213 if to_field.has_default() and (
1214 # Don't ignore a parent's auto-generated key if it's not the
1215 # parent model's pk and form data is provided.
1216 to_field.attname == self.fk.remote_field.model._meta.pk.name
1217 or not form.data
1218 ):
1219 setattr(self.instance, to_field.attname, None)
1220
1221 form.fields[name] = InlineForeignKeyField(self.instance, **kwargs)
1222
1223 def get_unique_error_message(self, unique_check):
1224 unique_check = [field for field in unique_check if field != self.fk.name]

Callers

nothing calls this directly

Calls 6

capfirstFunction · 0.90
has_defaultMethod · 0.80
add_fieldsMethod · 0.45
getMethod · 0.45
get_fieldMethod · 0.45

Tested by

no test coverage detected