Add has_coordinates filter to Site #46

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

Originally created by @jirivrany on 3/31/2026

Proposed functionality

Add a filter to the Site list that allows users to filter by whether GPS coordinates are set.

The Site model already has latitude and longitude fields (both nullable DecimalField). The proposed changes are minimal — no migrations, no new imports:

dcim/filtersets.py — add to SiteFilterSet:

has_coordinates = django_filters.BooleanFilter(
    method='_has_coordinates',
    label=_('Has coordinates'),
)

def _has_coordinates(self, queryset, name, value):
    params = Q(latitude__isnull=False) & Q(longitude__isnull=False)
    if value:
        return queryset.filter(params)
    return queryset.exclude(params)

dcim/forms/filtersets.py — add to SiteFilterForm:

has_coordinates = forms.NullBooleanField(
    required=False,
    label=_('Has coordinates'),
    widget=forms.Select(
        choices=BOOLEAN_WITH_BLANK_CHOICES
    )
)

And add 'has_coordinates' to the Attributes fieldset.

dcim/tests/test_filtersets.py — add to SiteTestCase:

def test_has_coordinates(self):
    params = {'has_coordinates': 'true'}
    self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
    params = {'has_coordinates': 'false'}
    self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

Use case

When managing a large number of Sites, operators need to quickly identify which Sites are missing GPS coordinates — for example, before placing them on a map or running a geo-based report. Currently there is no way to filter for this in the Site list without exporting and post-processing the data.

Why this belongs in core

latitude and longitude are core Site fields. Filtering by their null-ness is a natural and expected operation, consistent with how NetBox already filters other nullable fields (e.g. rack__isnull, tenant__isnull). No new fields, models, or migrations are required — only a filter method and a form field. The implementation mirrors the existing has_primary_ip filter on DeviceFilterSet.

*Originally created by @jirivrany on 3/31/2026* ## Proposed functionality Add a filter to the Site list that allows users to filter by whether GPS coordinates are set. The `Site` model already has `latitude` and `longitude` fields (both nullable `DecimalField`). The proposed changes are minimal — no migrations, no new imports: **`dcim/filtersets.py`** — add to `SiteFilterSet`: ```python has_coordinates = django_filters.BooleanFilter( method='_has_coordinates', label=_('Has coordinates'), ) def _has_coordinates(self, queryset, name, value): params = Q(latitude__isnull=False) & Q(longitude__isnull=False) if value: return queryset.filter(params) return queryset.exclude(params) ``` **`dcim/forms/filtersets.py`** — add to `SiteFilterForm`: ```python has_coordinates = forms.NullBooleanField( required=False, label=_('Has coordinates'), widget=forms.Select( choices=BOOLEAN_WITH_BLANK_CHOICES ) ) ``` And add `'has_coordinates'` to the `Attributes` fieldset. **`dcim/tests/test_filtersets.py`** — add to `SiteTestCase`: ```python def test_has_coordinates(self): params = {'has_coordinates': 'true'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) params = {'has_coordinates': 'false'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) ``` ## Use case When managing a large number of Sites, operators need to quickly identify which Sites are missing GPS coordinates — for example, before placing them on a map or running a geo-based report. Currently there is no way to filter for this in the Site list without exporting and post-processing the data. ## Why this belongs in core `latitude` and `longitude` are core Site fields. Filtering by their null-ness is a natural and expected operation, consistent with how NetBox already filters other nullable fields (e.g. `rack__isnull`, `tenant__isnull`). No new fields, models, or migrations are required — only a filter method and a form field. The implementation mirrors the existing `has_primary_ip` filter on `DeviceFilterSet`.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#46