API unable to filter sites on region when region is null (not present) #2749

Closed
opened 2025-12-29 18:21:44 +01:00 by adam · 5 comments
Owner

Originally created by @Renji-FR on GitHub (Jul 22, 2019).

Originally assigned to: @kobayashi on GitHub.

Environment

  • Python version: 3.6.9
  • NetBox version: 59139502ac7d (v2.6.1)

Steps to Reproduce

  1. Create a site without region as parent
{
            "id": 2,
            "name": "Without region",
            "slug": "without-region",
            "status": {
                "value": 1,
                "label": "Active"
            },
            "region": null,
            ...
}
  1. Try to get this site by site API with region filter to null
    url -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/dcim/sites/?region=null"
  2. Same result with region_id field filter

Expected Behavior

Site without region must be returned in JSON format

Observed Behavior

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Server Error</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
    <meta charset="UTF-8">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-danger" style="margin-top: 200px">
                    <div class="panel-heading">
                        <strong>
                            <i class="fa fa-warning"></i>
                            Server Error
                        </strong>
                    </div>
                    <div class="panel-body">
                        
                            <p>
                                There was a problem with your request. Please contact an administrator.
                            </p>
                        
                        <hr />
                        <p>
                            The complete exception is provided below:
                        </p>
<pre><strong>&lt;class &#39;AttributeError&#39;&gt;</strong><br />
&#39;str&#39; object has no attribute &#39;get_descendants&#39;</pre>
                        <p>
                            If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>.
                        </p>
                        <div class="text-right">
                            <a href="/" class="btn btn-primary">Home Page</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
Originally created by @Renji-FR on GitHub (Jul 22, 2019). Originally assigned to: @kobayashi on GitHub. ### Environment * Python version: 3.6.9 * NetBox version: 59139502ac7d (v2.6.1) ### Steps to Reproduce 1. Create a site without region as parent ```JSON { "id": 2, "name": "Without region", "slug": "without-region", "status": { "value": 1, "label": "Active" }, "region": null, ... } ``` 2. Try to get this site by site API with region filter to null ` url -H "Accept: application/json; indent=4" -H "Authorization: Token 0123456789abcdef0123456789abcdef01234567" "http://localhost:32768/api/dcim/sites/?region=null" ` 3. Same result with region_id field filter ### Expected Behavior Site without region must be returned in JSON format ### Observed Behavior ```HTML <!DOCTYPE html> <html lang="en"> <head> <title>Server Error</title> <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css"> <meta charset="UTF-8"> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="panel panel-danger" style="margin-top: 200px"> <div class="panel-heading"> <strong> <i class="fa fa-warning"></i> Server Error </strong> </div> <div class="panel-body"> <p> There was a problem with your request. Please contact an administrator. </p> <hr /> <p> The complete exception is provided below: </p> <pre><strong>&lt;class &#39;AttributeError&#39;&gt;</strong><br /> &#39;str&#39; object has no attribute &#39;get_descendants&#39;</pre> <p> If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>. </p> <div class="text-right"> <a href="/" class="btn btn-primary">Home Page</a> </div> </div> </div> </div> </div> </div> </body> </html> ```
adam added the type: bugstatus: accepted labels 2025-12-29 18:21:44 +01:00
adam closed this issue 2025-12-29 18:21:44 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 23, 2019):

Please correct your Python version. NetBox v2.6 requires Python 3.5 or higher.

@jeremystretch commented on GitHub (Jul 23, 2019): Please correct your Python version. NetBox v2.6 requires Python 3.5 or higher.
Author
Owner

@Renji-FR commented on GitHub (Jul 23, 2019):

I am sorry, I had checked my python version on my host and not in containers:

sudo docker exec -ti netbox_netbox-worker_1 /bin/bash
bash-4.4# python --version
Python 3.6.9
bash-4.4# exit
exit

sudo docker exec -ti netbox_netbox_1 /bin/bash       
bash-4.4# python --version
Python 3.6.9
bash-4.4# exit
exit
@Renji-FR commented on GitHub (Jul 23, 2019): I am sorry, I had checked my python version on my host and not in containers: ``` sudo docker exec -ti netbox_netbox-worker_1 /bin/bash bash-4.4# python --version Python 3.6.9 bash-4.4# exit exit sudo docker exec -ti netbox_netbox_1 /bin/bash bash-4.4# python --version Python 3.6.9 bash-4.4# exit exit ```
Author
Owner

@kobayashi commented on GitHub (Oct 10, 2019):

This is caused by failed in trying get_descendants "null" which type is str in TreeNodeMultipleChoiceFilter.
Before fixing this, I just want to know the purpose of TreeNodeMultipleChoiceFilter for slugish field? @jeremystretch
Why region field use TreeNodeMultipleChoiceFilter, but tenant field use django_filters.ModelMultipleChoiceFilter in SiteFilter

The easiest way is to change region field to the same django_filters.ModelMultipleChoiceFilter as tenant field.

@kobayashi commented on GitHub (Oct 10, 2019): This is caused by failed in trying get_descendants "null" which type is `str` in [`TreeNodeMultipleChoiceFilter`](https://github.com/netbox-community/netbox/blob/e223c885483da724840ced12e93ad70a4359da16/netbox/utilities/filters.py#L61). Before fixing this, I just want to know the purpose of `TreeNodeMultipleChoiceFilter` for slugish field? @jeremystretch Why [region field](https://github.com/netbox-community/netbox/blob/e223c885483da724840ced12e93ad70a4359da16/netbox/dcim/filters.py#L59) use `TreeNodeMultipleChoiceFilter`, but [tenant field](https://github.com/netbox-community/netbox/blob/e223c885483da724840ced12e93ad70a4359da16/netbox/tenancy/filtersets.py#L23) use `django_filters.ModelMultipleChoiceFilter` in `SiteFilter` The easiest way is to change region field to the same `django_filters.ModelMultipleChoiceFilter` as tenant field.
Author
Owner

@DanSheps commented on GitHub (Oct 10, 2019):

Region's is a modified pre-order Traversal Tree (MPTT), that is why we use the TreeNodeMultipleChoiceFilter

@DanSheps commented on GitHub (Oct 10, 2019): Region's is a modified pre-order Traversal Tree (MPTT), that is why we use the `TreeNodeMultipleChoiceFilter`
Author
Owner

@kobayashi commented on GitHub (Oct 14, 2019):

Thanks to let me know @DanSheps .I can understand.
This is to display sites belonging to nested regions also when filtering by the parent region.
I will make PR for this later.

@kobayashi commented on GitHub (Oct 14, 2019): Thanks to let me know @DanSheps .I can understand. This is to display sites belonging to nested regions also when filtering by the parent region. I will make PR for this later.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2749