Custom validators unable to validate tag presence #8124

Closed
opened 2025-12-29 20:32:44 +01:00 by adam · 6 comments
Owner

Originally created by @stuntguy3000 on GitHub (May 26, 2023).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.5.1

Python version

3.10

Steps to Reproduce

  1. Set the Custom Validators setting to validate tag requirements
CUSTOM_VALIDATORS = {
        "ipam.ipaddress": [{"tags": {"required": True}}],
        "dcim.site": [{"tags": {"required": True}}],
        "dcim.region": [{"tags": {"required": True}}]
}
  1. Create a new object defined in the list above with/without tags defined.

(This error is not exclusive to those object types)

Expected Behavior

I expected NetBox to validate if that particular object has tags set.

Observed Behavior

<class 'ValueError'>

IPAddress objects need to have a primary key value before you can access their tags.

Python version: 3.10.6
NetBox version: 3.5.1

I'm not sure if I'm asking too much from the Custom Validators here, or if another method to enforce the existence of Tags is available.

Originally created by @stuntguy3000 on GitHub (May 26, 2023). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.5.1 ### Python version 3.10 ### Steps to Reproduce 1. Set the Custom Validators setting to validate tag requirements ``` CUSTOM_VALIDATORS = { "ipam.ipaddress": [{"tags": {"required": True}}], "dcim.site": [{"tags": {"required": True}}], "dcim.region": [{"tags": {"required": True}}] } ``` 2. Create a new object defined in the list above with/without tags defined. _(This error is not exclusive to those object types)_ ### Expected Behavior I expected NetBox to validate if that particular object has tags set. ### Observed Behavior ``` <class 'ValueError'> IPAddress objects need to have a primary key value before you can access their tags. Python version: 3.10.6 NetBox version: 3.5.1 ``` I'm not sure if I'm asking too much from the Custom Validators here, or if another method to enforce the existence of Tags is available.
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 20:32:44 +01:00
adam closed this issue 2025-12-29 20:32:44 +01:00
Author
Owner

@stuntguy3000 commented on GitHub (May 31, 2023):

I'm curious if this is actually fixable?

My understanding is that the Model is passed to the custom validator, but the tag information is lost as it sits as it is different from the other fields. Validators can't access the raw form data and the tags input is not passed along down this chain.

The only fix I can even see is to either hack together more custom validation specifically for tags, or to save the object BEFORE the tags are then validated - what could go wrong.

Have to leave this for someone more versed in Django, this isn't a simple fix :(

@stuntguy3000 commented on GitHub (May 31, 2023): I'm curious if this is actually fixable? My understanding is that the Model is passed to the custom validator, but the tag information is lost as it sits as it is _different_ from the other fields. Validators can't access the raw form data and the tags input is not passed along down this chain. The only fix I can even see is to either hack together more custom validation specifically for tags, or to save the object BEFORE the tags are then validated - what could go wrong. Have to leave this for someone more versed in Django, this isn't a simple fix :(
Author
Owner

@netopsab commented on GitHub (Jun 24, 2023):

Hi,

This is the only way I have found to catch tags (or other m2m fields) with CustomValidator :

from netbox.context import current_request
from extras.models import Tag

request = current_request.get()
tags = Tag.objects.filter(id__in=request.POST.getlist('tags')).values_list('name', flat=True)

Note : this is for the 'standard' edit form. For bulk edit (or import form), you have to dig with other fields (like add_tags, remove_tags) and check the view name before (with object request.resolver_match.view_name).

from extras.models import Tag

tags = set(instance.tags.names()).union(
    Tag.objects.filter(id__in=request.POST.getlist('add_tags')).values_list('name', flat=True)
).difference(
    Tag.objects.filter(id__in=request.POST.getlist('remove_tags')).values_list('name', flat=True)
)

Hope this can help you.

NB : for tracking, this issue occurs for all m2m fields (like tagged_vlans), not just tags.

@netopsab commented on GitHub (Jun 24, 2023): Hi, This is the only way I have found to catch tags (or other m2m fields) with CustomValidator : ```python from netbox.context import current_request from extras.models import Tag request = current_request.get() tags = Tag.objects.filter(id__in=request.POST.getlist('tags')).values_list('name', flat=True) ``` Note : this is for the 'standard' edit form. For bulk edit (or import form), you have to dig with other fields (like add_tags, remove_tags) and check the view name before (with object `request.resolver_match.view_name`). ```python from extras.models import Tag tags = set(instance.tags.names()).union( Tag.objects.filter(id__in=request.POST.getlist('add_tags')).values_list('name', flat=True) ).difference( Tag.objects.filter(id__in=request.POST.getlist('remove_tags')).values_list('name', flat=True) ) ``` Hope this can help you. NB : for tracking, this issue occurs for all m2m fields (like tagged_vlans), not just tags.
Author
Owner

@github-actions[bot] commented on GitHub (Sep 27, 2023):

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

@github-actions[bot] commented on GitHub (Sep 27, 2023): This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. **Do not** attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our [contributing guide](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@github-actions[bot] commented on GitHub (Nov 2, 2023):

This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.

@github-actions[bot] commented on GitHub (Nov 2, 2023): This issue has been automatically closed due to lack of activity. In an effort to reduce noise, please do not comment any further. Note that the core maintainers may elect to reopen this issue at a later date if deemed necessary.
Author
Owner

@arthanson commented on GitHub (Nov 3, 2023):

@Urth stated he wanted to work on this.

@arthanson commented on GitHub (Nov 3, 2023): @Urth stated he wanted to work on this.
Author
Owner

@Urth commented on GitHub (Nov 3, 2023):

yes, please assign it to me 👍

@Urth commented on GitHub (Nov 3, 2023): yes, please assign it to me :+1:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8124