Closes #21201: Support GenericForeignKey fields when cloning objects #446

Closed
opened 2026-04-05 16:33:08 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @pheus on 1/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!

*Originally created by @pheus on 1/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!
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#446