MCPcopy
hub / github.com/django/django / InlineForeignKeyField

Class InlineForeignKeyField

django/forms/models.py:1376–1417  ·  view source on GitHub ↗

A basic integer field that deals with validating the given value to a given parent instance in an inline.

Source from the content-addressed store, hash-verified

1374
1375
1376class InlineForeignKeyField(Field):
1377 """
1378 A basic integer field that deals with validating the given value to a
1379 given parent instance in an inline.
1380 """
1381
1382 widget = HiddenInput
1383 default_error_messages = {
1384 "invalid_choice": _("The inline value did not match the parent instance."),
1385 }
1386
1387 def __init__(self, parent_instance, *args, pk_field=False, to_field=None, **kwargs):
1388 self.parent_instance = parent_instance
1389 self.pk_field = pk_field
1390 self.to_field = to_field
1391 if self.parent_instance is not None:
1392 if self.to_field:
1393 kwargs["initial"] = getattr(self.parent_instance, self.to_field)
1394 else:
1395 kwargs["initial"] = self.parent_instance.pk
1396 kwargs["required"] = False
1397 super().__init__(*args, **kwargs)
1398
1399 def clean(self, value):
1400 if value in self.empty_values:
1401 if self.pk_field:
1402 return None
1403 # if there is no value act as we did before.
1404 return self.parent_instance
1405 # ensure the we compare the values as equal types.
1406 if self.to_field:
1407 orig = getattr(self.parent_instance, self.to_field)
1408 else:
1409 orig = self.parent_instance.pk
1410 if str(value) != str(orig):
1411 raise ValidationError(
1412 self.error_messages["invalid_choice"], code="invalid_choice"
1413 )
1414 return self.parent_instance
1415
1416 def has_changed(self, initial, data):
1417 return False
1418
1419
1420class ModelChoiceIteratorValue:

Callers 1

add_fieldsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…