-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom validators unable to validate tag presence #12731
Comments
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 :( |
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 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. |
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. |
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. |
…d serializers This allows for validation of any m2m relation like tags. The validator receives the cleaned data from the form/serializer. The model instance is not consistently available in the model forms/serializers so is ommitted from both. Note: the available fields can vary per form/serializer for a model. You should always check if a field is defined in the cleaned data.
…d serializers This allows for validation of any m2m relation like tags. The validator receives the cleaned data from the form/serializer. The model instance is not consistently available in the model forms/serializers so is omitted from both. Note: the available fields can vary per form/serializer for a model. You should always check if a field is defined in the cleaned data.
@Urth stated he wanted to work on this. |
yes, please assign it to me 👍 |
NetBox version
v3.5.1
Python version
3.10
Steps to Reproduce
(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
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.
The text was updated successfully, but these errors were encountered: