ModelState should be inspected to determine whether an object is being created or updated #17577
Labels
status: accepted
This issue has been accepted for implementation
type: housekeeping
Changes to the application which do not directly impact the end user
Proposed Changes
Anywhere it's necessary to determine whether an object is being created or updated (e.g. inside
clean()
), we should inspect the instance's_state
attribute, which is a ModelState object. The_adding
boolean of this object indicates whether the instance is being added to the database or updated.Justification
In many places around the application, we use
if self.pk
as a shorthand for determining whether an object has already been created in the database. (If not, its primary key will not yet be set.) However, this assumes that the primary key can never be known in advance, which may not necessarily be true. For instance, the netbox-branching plugin copies predetermined primary keys when creating an object.The text was updated successfully, but these errors were encountered: