Custom script, filter on tags to VLAN

449 views
Skip to first unread message

Mats Brorson

unread,
Apr 19, 2021, 9:22:09 AM4/19/21
to NetBox

This have to be simple. Use case:

- A bunch of VLANs are reserved
- We use tags to define for what purpouse we have reserved them, and what system they are reserved for etc
- We have therefore a need in our automation to filter on more than one tag with no user input

Filter on one tag i simple (using nbshell to mess about):

VLAN.objects.filter(status='reserved', tags__name='eBGP')

But how do I filter in this case, so I get all VLANs that have bothe the 'eBGP' tag AND for example 'OpenStack'?



Brian Candler

unread,
Apr 19, 2021, 10:00:12 AM4/19/21
to NetBox
For the OR condition, do this:

VLAN.objects.filter(tags__name__in=['eBGP', 'OpenStack'])

For the AND condition, do this:

VLAN.objects.filter(tags__name='eBGP').filter(tags__name=OpenStack')

Mats Brorson

unread,
Apr 19, 2021, 1:05:33 PM4/19/21
to NetBox
Dang it, I was thinking about nested filters, but dismissed it before trying. Brian, you are a hero!
Reply all
Reply to author
Forward
0 new messages