nautobot.apps.filters
¶
Filterset base classes and mixins for app implementation.
nautobot.apps.filters.BaseFilterSet
¶
Bases: django_filters.FilterSet
A base filterset which provides common functionality to all Nautobot filtersets.
Source code in nautobot/core/filters.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
|
errors
property
¶
Extend FilterSet.errors to potentially include additional errors from settings.STRICT_FILTERING.
add_filter(new_filter_name, new_filter_field)
classmethod
¶
Allow filters to be added post-generation on import.
Will provide <field_name>__<lookup_expr>
generation automagically.
Source code in nautobot/core/filters.py
filter_for_lookup(field, lookup_type)
classmethod
¶
Override filter_for_lookup method to set ChoiceField Filter to MultipleChoiceFilter.
Note: Any CharField or IntegerField with choices set is a ChoiceField.
Source code in nautobot/core/filters.py
get_filters()
classmethod
¶
Override filter generation to support dynamic lookup expressions for certain filter types.
Source code in nautobot/core/filters.py
is_valid()
¶
Extend FilterSet.is_valid() to potentially enforce settings.STRICT_FILTERING.
Source code in nautobot/core/filters.py
nautobot.apps.filters.ConfigContextRoleFilter
¶
Bases: NaturalKeyOrPKMultipleChoiceFilter
Limit role choices to the available role choices for Device and VM
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.ContentTypeChoiceFilter
¶
Bases: ContentTypeFilterMixin
, django_filters.ChoiceFilter
Allows character-based ContentType filtering by
Example use on a FilterSet
:
content_type = ContentTypeChoiceFilter(
choices=FeatureQuery("dynamic_groups").get_choices,
)
Source code in nautobot/core/filters.py
nautobot.apps.filters.ContentTypeFilter
¶
Bases: ContentTypeFilterMixin
, django_filters.CharFilter
Allows character-based ContentType filtering by
Does not support limiting of choices. Can be used without arguments on a FilterSet
:
content_type = ContentTypeFilter()
Source code in nautobot/core/filters.py
nautobot.apps.filters.ContentTypeFilterMixin
¶
Mixin to allow specifying a ContentType by
Source code in nautobot/core/filters.py
nautobot.apps.filters.ContentTypeMultipleChoiceFilter
¶
Bases: django_filters.MultipleChoiceFilter
Allows multiple-choice ContentType filtering by
Defaults to joining multiple options with "AND". Pass conjoined=False
to
override this behavior to join with "OR" instead.
Example use on a FilterSet
:
content_types = ContentTypeMultipleChoiceFilter(
choices=FeatureQuery("statuses").get_choices,
)
Source code in nautobot/core/filters.py
filter(qs, value)
¶
Filter on value, which should be list of content-type names.
e.g. ['dcim.device', 'dcim.rack']
Source code in nautobot/core/filters.py
nautobot.apps.filters.CustomFieldModelFilterSetMixin
¶
Bases: django_filters.FilterSet
Dynamically add a Filter for each CustomField applicable to the parent model. Add filters for extra lookup expressions on supported CustomField types.
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.FilterExtension
¶
Class that may be returned by a registered Filter Extension function.
Source code in nautobot/extras/plugins/__init__.py
nautobot.apps.filters.MappedPredicatesFilterMixin
¶
A filter mixin to provide the ability to specify fields and lookup expressions to use for filtering.
A mapping of filter predicates (field_name: lookup_expr) must be provided to the filter when
declared on a filterset. This mapping is used to construct a Q
query to filter based on the
provided predicates.
By default a predicate for {"id": "iexact"}
(id__exact
) will always be included.
Example
q = SearchFilter( filter_predicates={ "comments": "icontains", "name": "icontains", }, )
Optionally you may also provide a callable to use as a preprocessor for the filter predicate by providing the value as a nested dict with "lookup_expr" and "preprocessor" keys. For example:
q = SearchFilter(
filter_predicates={
"asn": {
"lookup_expr": "exact",
"preprocessor": int,
},
},
)
This tells the filter to try to cast asn
to an int
. If it fails, this predicate will be
skipped.
Source code in nautobot/core/filters.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
|
generate_query(value, **kwargs)
¶
Given a value
, return a Q
object for 2-tuple of predicate=value
. Filter predicates are
read from the instance filter. Any kwargs
are ignored.
Source code in nautobot/core/filters.py
nautobot.apps.filters.MultiValueBigNumberFilter
¶
nautobot.apps.filters.NameSearchFilterSet
¶
Bases: django_filters.FilterSet
A base class for adding the search method to models which only expose the name
field in searches.
Source code in nautobot/core/filters.py
nautobot.apps.filters.NaturalKeyOrPKMultipleChoiceFilter
¶
Bases: django_filters.ModelMultipleChoiceFilter
Filter that supports filtering on values matching the pk
field and another
field of a foreign-key related object. The desired field is set using the to_field_name
keyword argument on filter initialization (defaults to name
).
Source code in nautobot/core/filters.py
get_filter_predicate(v)
¶
Override base filter behavior to force the filter to use the pk
field instead of
the natural key in the generated filter.
Source code in nautobot/core/filters.py
nautobot.apps.filters.NautobotFilterSet
¶
Bases: BaseFilterSet
, CreatedUpdatedModelFilterSetMixin
, RelationshipModelFilterSetMixin
, CustomFieldModelFilterSetMixin
This class exists to combine common functionality and is used as a base class throughout the codebase where all of BaseFilterSet, CreatedUpdatedModelFilterSetMixin, RelationshipModelFilterSetMixin and CustomFieldModelFilterSetMixin are needed.
Source code in nautobot/extras/filters/__init__.py
nautobot.apps.filters.NumericArrayFilter
¶
Bases: django_filters.NumberFilter
Filter based on the presence of an integer within an ArrayField.
Source code in nautobot/core/filters.py
nautobot.apps.filters.RelatedMembershipBooleanFilter
¶
Bases: django_filters.BooleanFilter
BooleanFilter for related objects that will explicitly perform exclude=True
and isnull
lookups. The field_name
argument is required and must be set to the related field on the
model.
This should be used instead of a default BooleanFilter
paired method=
argument to test for the existence of related objects.
Example
has_interfaces = RelatedMembershipBooleanFilter( field_name="interfaces", label="Has interfaces", )
Source code in nautobot/core/filters.py
nautobot.apps.filters.RelationshipFilter
¶
Bases: django_filters.ModelMultipleChoiceFilter
Filter objects by the presence of associations on a given Relationship.
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.RelationshipModelFilterSetMixin
¶
Bases: django_filters.FilterSet
Filterset for relationships applicable to the parent model.
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.RoleFilter
¶
Bases: NaturalKeyOrPKMultipleChoiceFilter
Limit role choices to the available role choices for self.model
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.RoleModelFilterSetMixin
¶
nautobot.apps.filters.SearchFilter
¶
Bases: MappedPredicatesFilterMixin
, django_filters.CharFilter
Provide a search filter for use on filtersets as the q=
parameter.
See the docstring for nautobot.core.filters.MappedPredicatesFilterMixin
for usage.
Source code in nautobot/core/filters.py
nautobot.apps.filters.StatusFilter
¶
Bases: django_filters.ModelMultipleChoiceFilter
Filter field used for filtering Status fields.
Explicitly sets to_field_name='value'
and dynamically sets queryset to
retrieve choices for the corresponding model & field name bound to the
filterset.
Source code in nautobot/extras/filters/mixins.py
get_filter_predicate(value)
¶
Always use the field's name and the to_field_name
attribute as predicate.
Source code in nautobot/extras/filters/mixins.py
nautobot.apps.filters.StatusModelFilterSetMixin
¶
nautobot.apps.filters.TagFilter
¶
Bases: NaturalKeyOrPKMultipleChoiceFilter
Match on one or more assigned tags. If multiple tags are specified (e.g. ?tag=foo&tag=bar), the queryset is filtered to objects matching all tags.
Source code in nautobot/core/filters.py
__init__(*args, **kwargs)
¶
Source code in nautobot/core/filters.py
nautobot.apps.filters.TenancyModelFilterSetMixin
¶
Bases: django_filters.FilterSet
An inheritable FilterSet for models which support Tenant assignment.
Source code in nautobot/tenancy/filters/mixins.py
nautobot.apps.filters.TreeNodeMultipleChoiceFilter
¶
Bases: NaturalKeyOrPKMultipleChoiceFilter
Filter that matches on the given model(s) (identified by name and/or pk) as well as their tree descendants.
For example, if we have:
Location "Earth"
Location "USA"
Location "GA" <- Location "Athens"
Location "NC" <- Location "Durham"
a NaturalKeyOrPKMultipleChoiceFilter on Location for {"parent": "USA"} would only return "GA" and "NC" since that is the only two locations that have an immediate parent "USA" but a TreeNodeMultipleChoiceFilter on Location for {"parent": "USA"} would match both "Athens" and "Durham" in addition to "GA" and "NC".
Source code in nautobot/core/filters.py
generate_query(value, qs=None, **kwargs)
¶
Given a filter value, return a Q
object that accounts for nested tree node descendants.
Source code in nautobot/core/filters.py
nautobot.apps.filters.multivalue_field_factory(field_class, widget=django_forms.SelectMultiple)
¶
Given a form field class, return a subclass capable of accepting multiple values. This allows us to OR on multiple filter values while maintaining the field's built-in validation. Example: GET /api/dcim/devices/?name=foo&name=bar