Skip to content

Conversation

@pheus
Copy link
Contributor

@pheus pheus commented Jan 29, 2026

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>_type and <name>_id). In the UI, however, NetBox forms typically expose a single combined DynamicModelChoiceField named 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

  • Enhance CloningMixin.clone() to detect GenericForeignKey fields and, when both the content type field and ID field are included in clone_fields, inject the GFK attribute name into the returned clone dict (unless already set).
  • Add tests covering:
    • successful injection when both constituent fields are present
    • no injection when only one constituent field is present

Result

Cloning now pre-populates form fields backed by GenericForeignKey consistently across the UI without requiring model- or form-specific overrides.

Thanks for reviewing!

Extend the CloningMixin to inject GenericForeignKey (GFK) attributes
when both content type and ID fields are present. Improves support for
models using GFK fields during cloning operations.

Fixes #21201
@pheus pheus marked this pull request as ready for review January 30, 2026 14:18
@pheus pheus requested review from a team and jeremystretch and removed request for a team January 30, 2026 14:18
Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pheus!

@jeremystretch jeremystretch merged commit b26c7f3 into main Feb 2, 2026
10 checks passed
@jeremystretch jeremystretch deleted the 21201-pre-populate-genericforeignkey-form-fields-when-cloning branch February 2, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pre-populate GenericForeignKey form fields when cloning

3 participants