Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelState should be inspected to determine whether an object is being created or updated #17577

Closed
jeremystretch opened this issue Sep 23, 2024 · 0 comments · Fixed by #17583
Assignees
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user

Comments

@jeremystretch
Copy link
Member

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.

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user labels Sep 23, 2024
@jeremystretch jeremystretch self-assigned this Sep 23, 2024
jeremystretch added a commit that referenced this issue Sep 23, 2024
jeremystretch added a commit that referenced this issue Sep 24, 2024
peteeckel added a commit to peteeckel/netbox-plugin-dns that referenced this issue Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
1 participant