Add Add-button on Typed tabs with reverse-reference prefill (closes #9)#10
Open
Add Add-button on Typed tabs with reverse-reference prefill (closes #9)#10
Conversation
Each typed tab now exposes an "Add <Type>" button that links to the native customobject_add view with the back-reference field pre-populated to the parent object's PK and return_url set to the current tab path. After saving, the user lands back on the tab with filters preserved. When a Custom Object Type has multiple back-reference fields to the same parent model (e.g. primary_device + backup_device both -> dcim.device), the button becomes a Bootstrap split-dropdown listing each field. The button is hidden for users without add_customobject permission. Implementation: - New module-level _build_add_links() helper computes URLs from (slug, instance_pk, field_infos, return_url). Pure function, fully unit-tested. - field_infos tuples extended from (name, type) to (name, type, label) so the dropdown can show human-readable field labels. Star-unpacking in _count_for_type and the queryset filter loop preserves backward compatibility with 2-tuple shapes used by existing tests. - Permission gate uses utilities.permissions.get_permission_for_model, matching the pattern used by netbox_custom_objects.tables.CustomObjectActionsColumn. - field_infos is sorted by field name in register_typed_tabs so the dropdown order is deterministic. Tab placement: top-right of tab content, scoped entirely to typed/tab.html. Tabs with hide_if_empty=True remain hidden until the first object is created via the native menu - the button surfaces once the tab is visible. Bumps version to 2.2.0 (minor, new feature). Test suite extended from 53 to 61 tests covering reverse failure, single/multi field, deduplication, label fallback, 2-tuple back-compat, and return_url URL-encoding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
customobject_addview with the back-reference field pre-populated to the parent object's PK andreturn_urlset to the current tab path. One click + fill form + save → back on the tab, filters preserved.primary_deviceandbackup_deviceboth →dcim.device), the button becomes a Bootstrap split-dropdown listing each field ("via label"). Single button otherwise.add_customobjectpermission (viaget_permission_for_model(model, 'add')+user.has_perm(), mirroring the pattern used byCustomObjectActionsColumn).hide_if_empty=Trueis unchanged: the tab appears only once the first object is linked (via the native menu); subsequent additions use the new button.Design
_build_add_links(slug, pk, field_infos, return_url)helper — pure function, fully unit-tested. Produces[{field_name, label, url}].field_infostuples widened from(name, type)to(name, type, label)so the field label is resolved once at registration time instead of per-request. All call sites adjusted; existing 2-tuple tests remain green via*_unpacking.register_typed_tabssorts each group by field name for deterministic button ordering.ObjectEditView.get()doinginitial = normalize_querydict(request.GET). Works uniformly forTYPE_OBJECT(DynamicModelChoiceField) andTYPE_MULTIOBJECT(DynamicModelMultipleChoiceField).Test plan
_build_add_links(no-reverse fallback, single field, multi-field, dedup, label fallback, URL encoding without double-encoding).register_typed_tabslabel population + deterministic sort test.ruff check+ruff formatclean.add_customobjectperm sees no button.Closes #9.