Closes #21201: Support GenericForeignKey fields when cloning objects #21324
+69
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #21201
This PR improves cloning support for models that use GenericForeignKey (GFK) fields.
Problem
When cloning an object,
CloningMixin.clone()currently copies only the underlying database fields (e.g.<name>_typeand<name>_id). In the UI, however, NetBox forms typically expose a single combinedDynamicModelChoiceFieldnamed after the GFK attribute (e.g.parent,scope). As a result, the selector field is not pre-populated during cloning even though the CT+ID values are present in the URL parameters.Changes
CloningMixin.clone()to detectGenericForeignKeyfields and, when both the content type field and ID field are included inclone_fields, inject the GFK attribute name into the returned clone dict (unless already set).Result
Cloning now pre-populates form fields backed by
GenericForeignKeyconsistently across the UI without requiring model- or form-specific overrides.Thanks for reviewing!