question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test factory get_deletable_objects needs to check relationships of children of Tree objects

See original GitHub issue

Proposed Changes

nautobot.utilities.testing.utils.get_deletable_objects (in 1.5+) filters a queryset on protected fields fieldname__isnull=True but also needs to check any children of a tree object. If a child’s fieldname is not null, you cannot delete the parent.

Justification

Reuse testing code for testing object deletion

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
gsnider2195commented, Oct 19, 2022

@HanlinMiao try this

from tree_queries.models import TreeNodeForeignKey
from mptt.models import TreeForeignKey
def get_deletable_objects(model, queryset):
    """
    Returns a queryset of objects in the supplied queryset that have no protected relationships that would prevent deletion.
    """
    q = Q()
    for field in model._meta.get_fields(include_parents=True):
        if getattr(field, "on_delete", None) is PROTECT:
            q &= Q(**{f"{field.name}__isnull": True})
        # Only delete leaf nodes of trees to reduce complexity
        if isinstance(field, (TreeForeignKey, TreeNodeForeignKey)):
            q &= Q(**{f"{field.related_query_name()}__isnull": True})
    return queryset.filter(q)
0reactions
gsnider2195commented, Oct 19, 2022

The above suggestion breaks on InventoryItem because the children reverse relationship name is child_items instead of children.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Check for Children Sum Property in a Binary Tree
Given a binary tree, the task is to check for every node, its value is equal to the sum of values of its...
Read more >
Everything you need to know about tree data structures
Imagine a family tree with relationships from all generation: grandparents, parents, children, siblings, etc. We commonly organize family trees ...
Read more >
Tree-Like Objects - TDD Patterns - XP123
A tree is a natural way to represent a hierarchy. We'll look at various examples, and ways to test-drive trees and their close...
Read more >
Trees in Java How to Implement a Binary Tree? - Edureka
A Tree is a non-linear data structure where data objects are generally organized in terms of hierarchical relationship.
Read more >
Introduction to data.tree
data.tree structure: a tree, consisting of multiple Node objects. ... linking them together so as to define the parent-child relationships.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found