nautobot.apps.api
¶
Helpers for an app to implement a REST API.
nautobot.apps.api.APIRootView
¶
Bases: NautobotAPIVersionMixin
, APIView
This is the root of the REST API. API endpoints are arranged by app and model name; e.g. /api/dcim/locations/
.
Source code in nautobot/core/api/views.py
nautobot.apps.api.BaseModelSerializer
¶
Bases: OptInFieldsMixin
, serializers.HyperlinkedModelSerializer
This base serializer implements common fields and logic for all ModelSerializers.
Namely, it:
- defines the
display
field which exposes a human friendly value for the given object. - ensures that
id
field is always present on the serializer as well. - ensures that
created
andlast_updated
fields are always present if applicable to this model and serializer. - ensures that
object_type
field is always present on the serializer which represents the content-type of this serializer's associated model (e.g. "dcim.device"). This is required as the OpenAPI schema, using the PolymorphicProxySerializer class defined below, relies upon this field as a way to identify to the client which of several possible serializers are in use for a given attribute. - supports
?depth
query parameter. It is passed in asnested_depth
to thebuild_nested_field()
function to enable the dynamic generation of nested serializers.
Source code in nautobot/core/api/serializers.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 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 |
|
is_nested
property
¶
Return whether this is a nested serializer.
build_field(field_name, info, model_class, nested_depth)
¶
Return a two tuple of (cls, kwargs) to build a serializer field with.
Source code in nautobot/core/api/serializers.py
build_property_field(field_name, model_class)
¶
Create a property field for model methods and properties.
Source code in nautobot/core/api/serializers.py
build_relational_field(field_name, relation_info)
¶
Override DRF's default relational-field construction to be app-aware.
Source code in nautobot/core/api/serializers.py
build_url_field(field_name, model_class)
¶
Override DRF's default 'url' field construction to be app-aware.
Source code in nautobot/core/api/serializers.py
determine_view_options(request=None)
¶
Determine view options to use for rendering the list and detail views associated with this serializer.
Source code in nautobot/core/api/serializers.py
extend_field_names(fields, field_name, at_start=False, opt_in_only=False)
¶
Prepend or append the given field_name to fields
and optionally self.Meta.opt_in_fields as well.
Source code in nautobot/core/api/serializers.py
get_additional_detail_view_tabs()
¶
Retrieve definitions of non-default detail view tabs.
By default provides an "Advanced" tab containing self.advanced_tab_fields
, but subclasses
can override this to move additional serializer fields to this or other tabs.
Returns:
Type | Description |
---|---|
dict
|
|
Source code in nautobot/core/api/serializers.py
get_display(instance)
¶
Return either the display
property of the instance or str(instance)
get_field_names(declared_fields, info)
¶
Override get_field_names() to add some custom logic.
Assuming that we follow the pattern where fields = "__all__" for the vast majority of serializers in Nautobot,
we do not strictly need to use this method to protect against inadvertently omitting standard fields
like
display,
created, and
last_updated`. However, we continue to do as a bit of redundant safety.
The other purpose of this method now is to manipulate the set of fields that "all" actually means as a way of excluding fields that we don't want to include by default for performance or data exposure reasons.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.BulkDestroyModelMixin
¶
Support bulk deletion of objects using the list endpoint for a model. Accepts a DELETE action with a list of one or more JSON objects, each specifying the UUID of an object to be deleted. For example:
DELETE /api/dcim/locations/ [ {"id": "3f01f169-49b9-42d5-a526-df9118635d62"}, {"id": "c27d6c5b-7ea8-41e7-b9dd-c065efd5d9cd"} ]
Source code in nautobot/core/api/views.py
nautobot.apps.api.BulkUpdateModelMixin
¶
Support bulk modification of objects using the list endpoint for a model. Accepts a PATCH action with a list of one or more JSON objects, each specifying the UUID of an object to be updated as well as the attributes to be set. For example:
PATCH /api/dcim/locations/ [ { "id": "1f554d07-d099-437d-8d48-7d6e35ec8fa3", "name": "New name" }, { "id": "1f554d07-d099-437d-8d48-7d6e65ec8fa3", "status": "planned" } ]
Source code in nautobot/core/api/views.py
nautobot.apps.api.ChoiceField
¶
Bases: serializers.Field
Represent a ChoiceField as {'value':
:param choices: An iterable of choices in the form (value, key). :param allow_blank: Allow blank values in addition to the listed choices.
Source code in nautobot/core/api/fields.py
nautobot.apps.api.ContentTypeField
¶
Bases: RelatedField
Represent a ContentType as '
Source code in nautobot/core/api/fields.py
nautobot.apps.api.CustomFieldModelSerializerMixin
¶
Bases: ValidatedModelSerializer
Extends ModelSerializer to render any CustomFields and their values associated with an object.
Source code in nautobot/core/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that "custom_fields" and "computed_fields" are included appropriately.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.CustomFieldModelViewSet
¶
Bases: ModelViewSet
Include the applicable set of CustomFields in the ModelViewSet context.
Source code in nautobot/extras/api/views.py
nautobot.apps.api.GetObjectCountsView
¶
Bases: NautobotAPIVersionMixin
, APIView
Enumerate the models listed on the Nautobot home page and return data structure containing verbose_name_plural, url and count.
Source code in nautobot/core/api/views.py
nautobot.apps.api.ModelViewSet
¶
Bases: NautobotAPIVersionMixin
, BulkUpdateModelMixin
, BulkDestroyModelMixin
, ModelViewSetMixin
, ModelViewSet_
Extend DRF's ModelViewSet to support bulk update and delete functions.
Source code in nautobot/core/api/views.py
nautobot.apps.api.ModelViewSetMixin
¶
Source code in nautobot/core/api/views.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
get_object()
¶
Extend rest_framework.generics.GenericAPIView.get_object to allow "pk" lookups to use a composite-key.
Source code in nautobot/core/api/views.py
initial(request, *args, **kwargs)
¶
Runs anything that needs to occur prior to calling the method handler.
Override of internal Django Rest Framework API.
Source code in nautobot/core/api/views.py
restrict_queryset(request, *args, **kwargs)
¶
Restrict the view's queryset to allow only the permitted objects for the given request.
Subclasses (such as nautobot.extras.api.views.JobModelViewSet) may wish to override this.
Called by initial(), below.
Source code in nautobot/core/api/views.py
nautobot.apps.api.MultipleChoiceJSONField
¶
Bases: serializers.MultipleChoiceField
A MultipleChoiceField that renders the received value as a JSON-compatible list rather than a set.
Source code in nautobot/extras/api/fields.py
__init__(**kwargs)
¶
Overload default choices handling to also accept a callable.
nautobot.apps.api.NautobotAutoSchema
¶
Bases: AutoSchema
Nautobot-specific extensions to drf-spectacular's AutoSchema.
Source code in nautobot/core/api/schema.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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 |
|
is_bulk_action
property
¶
Custom property for convenience.
is_partial_action
property
¶
Custom property for convenience.
get_description()
¶
Get the appropriate description for a given API endpoint.
By default, if a specific action doesn't have its own docstring, and neither does the view class,
drf-spectacular will walk up the MRO of the view class until it finds a docstring, and use that.
Most of our viewsets (for better or for worse) do not have docstrings, and so it'll find and use the generic
docstring of the NautobotModelViewSet
class, which isn't very useful to the end user. Instead of doing that,
we only use the docstring of the view itself (ignoring its parent class docstrings), or if none exists, we
make an attempt at rendering a basically accurate default description.
Source code in nautobot/core/api/schema.py
get_filter_backends()
¶
Nautobot's custom bulk operations, even though they return a list of records, are NOT filterable.
get_operation_id()
¶
Extend the base method to handle Nautobot's REST API bulk operations.
Without this extension, every one of our ModelViewSet classes will result in drf-spectacular complaining
about operationId collisions, e.g. between DELETE /api/dcim/devices/ and DELETE /api/dcim/devices/
With this extension, the bulk endpoints automatically get a different operation-id from the non-bulk endpoints.
Source code in nautobot/core/api/schema.py
get_request_serializer()
¶
Return the request serializer (used for describing/parsing the request payload) for this endpoint.
We override the default drf-spectacular behavior for the case where the endpoint describes a write request with required data (PATCH, POST, PUT). In those cases we replace FooSerializer with a dynamically-defined WritableFooSerializer class in order to more accurately represent the available options on write.
We also override for the case where the endpoint is one of Nautobot's custom bulk API endpoints, which require a list of serializers as input, rather than a single one.
Source code in nautobot/core/api/schema.py
get_response_serializers()
¶
Return the response serializer (used for describing the response payload) for this endpoint.
We override the default drf-spectacular behavior for the case where the endpoint describes a write request to a bulk endpoint, which returns a list of serializers, rather than a single one.
Source code in nautobot/core/api/schema.py
get_serializer_ref_name(serializer)
¶
Get the serializer's ref_name Meta attribute if set, or else derive a ref_name automatically.
Based on drf_yasg.utils.get_serializer_ref_name().
Source code in nautobot/core/api/schema.py
get_writable_class(serializer, bulk=False)
¶
Given a FooSerializer instance, look up or construct a [Bulk]WritableFooSerializer class if necessary.
If no [Bulk]WritableFooSerializer class is needed, returns None instead.
Source code in nautobot/core/api/schema.py
resolve_serializer(serializer, direction, bypass_extensions=False)
¶
Re-add required id
field on bulk_partial_update action.
drf-spectacular clears the required
list for any partial serializers in its _map_basic_serializer()
,
but Nautobot bulk partial updates require the id
field to be specified for each object to update.
Source code in nautobot/core/api/schema.py
nautobot.apps.api.NautobotCSVParser
¶
Bases: BaseParser
Counterpart to NautobotCSVRenderer - import CSV data.
Source code in nautobot/core/api/parsers.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
row_elements_to_data(counter, row, serializer)
¶
Parse a single row of CSV data (represented as a dict) into a dict suitable for consumption by the serializer.
TODO: it would be more elegant if our serializer fields knew how to deserialize the CSV data themselves; could we then literally have the parser just return list(reader) and not need this function at all?
Source code in nautobot/core/api/parsers.py
nautobot.apps.api.NautobotHyperlinkedRelatedField
¶
Bases: WritableSerializerMixin
, serializers.HyperlinkedRelatedField
Extend HyperlinkedRelatedField to include URL namespace-awareness, add 'object_type' field, and read composite-keys.
Source code in nautobot/core/api/fields.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
__init__(*args, **kwargs)
¶
Override DRF's namespace-unaware default view_name logic for HyperlinkedRelatedField.
DRF defaults to '{model_name}-detail' instead of '{app_label}:{model_name}-detail'.
Source code in nautobot/core/api/fields.py
to_internal_value(data)
¶
Convert potentially nested representation to a model instance.
Source code in nautobot/core/api/fields.py
to_representation(value)
¶
Convert URL representation to a brief nested representation.
Source code in nautobot/core/api/fields.py
nautobot.apps.api.NautobotModelSerializer
¶
Bases: RelationshipModelSerializerMixin
, CustomFieldModelSerializerMixin
, NotesSerializerMixin
, ValidatedModelSerializer
Base class to use for serializers based on OrganizationalModel or PrimaryModel.
Can also be used for models derived from BaseModel, so long as they support custom fields, notes, and relationships.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.NautobotModelViewSet
¶
Bases: NotesViewSetMixin
, CustomFieldModelViewSet
Base class to use for API ViewSets based on OrganizationalModel or PrimaryModel.
Can also be used for models derived from BaseModel, so long as they support Notes.
Source code in nautobot/extras/api/views.py
nautobot.apps.api.NotesSerializerMixin
¶
Bases: BaseModelSerializer
Extend Serializer with a notes
field.
Source code in nautobot/core/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that fields includes "notes_url" field if applicable.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.NotesViewSetMixin
¶
Source code in nautobot/extras/api/views.py
CreateNotePermissions
¶
Bases: TokenPermissions
As nautobot.core.api.authentication.TokenPermissions, but enforcing add_note permission.
Source code in nautobot/extras/api/views.py
notes(request, *args, **kwargs)
¶
API methods for returning or creating notes on an object.
Source code in nautobot/extras/api/views.py
restrict_queryset(request, *args, **kwargs)
¶
Apply "view" permissions on the POST /notes/ endpoint, otherwise as ModelViewSetMixin.
Source code in nautobot/extras/api/views.py
nautobot.apps.api.ObjectTypeField
¶
Bases: serializers.CharField
Represent the ContentType of this serializer's model as "
Source code in nautobot/core/api/fields.py
__init__(*args, read_only=True, source='*', **kwargs)
¶
Default read_only to True as this should never be a writable field.
Source code in nautobot/core/api/fields.py
to_representation(_value)
¶
Get the content-type of this serializer's model.
Implemented this way because _value
may be None when generating the schema.
Source code in nautobot/core/api/fields.py
nautobot.apps.api.OptInFieldsMixin
¶
A serializer mixin that takes an additional opt_in_fields
argument that controls
which fields should be displayed.
Source code in nautobot/core/api/serializers.py
fields
property
¶
Removes all serializer fields specified in a serializers opt_in_fields
list that aren't specified in the
include
query parameter.
As an example, if the serializer specifies that opt_in_fields = ["computed_fields"]
but computed_fields
is not specified in the ?include
query parameter, computed_fields
will be popped
from the list of fields.
nautobot.apps.api.OrderedDefaultRouter
¶
Bases: DefaultRouter
Source code in nautobot/core/api/routers.py
get_api_root_view(api_urls=None)
¶
Wrap DRF's DefaultRouter to return an alphabetized list of endpoints.
Source code in nautobot/core/api/routers.py
nautobot.apps.api.ReadOnlyModelViewSet
¶
Bases: NautobotAPIVersionMixin
, ModelViewSetMixin
, ReadOnlyModelViewSet_
Extend DRF's ReadOnlyModelViewSet to support queryset restriction.
nautobot.apps.api.RelationshipModelSerializerMixin
¶
Bases: ValidatedModelSerializer
Extend ValidatedModelSerializer with a relationships
field.
Source code in nautobot/core/api/serializers.py
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 |
|
get_field_names(declared_fields, info)
¶
Ensure that "relationships" is included as an opt-in field on root serializers.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.SerializedPKRelatedField
¶
Bases: PrimaryKeyRelatedField
Extends PrimaryKeyRelatedField to return a serialized object on read. This is useful for representing related objects in a ManyToManyField while still allowing a set of primary keys to be written.
Source code in nautobot/core/api/fields.py
nautobot.apps.api.TaggedModelSerializerMixin
¶
Bases: BaseModelSerializer
Source code in nautobot/extras/api/mixins.py
get_field_names(declared_fields, info)
¶
Ensure that 'tags' field is always present except on nested serializers.
Source code in nautobot/extras/api/mixins.py
nautobot.apps.api.TimeZoneSerializerField
¶
nautobot.apps.api.TreeModelSerializerMixin
¶
Bases: BaseModelSerializer
Add a tree_depth
field to non-nested model serializers based on django-tree-queries.
Source code in nautobot/core/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that "tree_depth" is included on root serializers only, as nested objects are not annotated.
Source code in nautobot/core/api/serializers.py
get_tree_depth(obj)
¶
The tree_depth
is not a database field, but an annotation automatically added by django-tree-queries.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.ValidatedModelSerializer
¶
Bases: BaseModelSerializer
Extends the built-in ModelSerializer to enforce calling full_clean() on a copy of the associated instance during validation. (DRF does not do this by default; see https://github.com/encode/django-rest-framework/issues/3144)
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.WritableNestedSerializer
¶
Bases: BaseModelSerializer
Returns a nested representation of an object on read, but accepts either the nested representation or the primary key value on write operations.
Note that subclasses will always have a read-only object_type
field, which represents the content-type of this
serializer's associated model (e.g. "dcim.device"). This is required as the OpenAPI schema, using the
PolymorphicProxySerializer class defined below, relies upon this field as a way to identify to the client
which of several possible nested serializers are in use for a given attribute.
Source code in nautobot/core/api/serializers.py
nautobot.apps.api.WritableSerializerMixin
¶
WritableSerializerMixin provides the to_internal_value() function. The function provides the ability to write API requests that identify unique objects based on combinations of fields other than the primary key. e.g: "parent": { "location_type__parent": {"name": "Campus"}, "parent__name": "Campus-29" } vs "parent": "10dff139-7333-46b0-bef6-f6a5a7b5497c"
Source code in nautobot/core/api/mixins.py
get_object(data, queryset)
¶
Retrieve an unique object based on a dictionary of data attributes and raise errors accordingly if the object is not found.
Source code in nautobot/core/api/mixins.py
get_queryset_filter_params(data, queryset)
¶
Data could be a dictionary and an int (for the User model) or a str that represents the primary key. If it is a dictionary, we return it after remove non-filter fields. If it is a primary key, we return a dictionary object formatted like this {"pk": pk}
Source code in nautobot/core/api/mixins.py
remove_non_filter_fields(filter_params)
¶
Make output from a WritableSerializer "round-trip" capable by automatically stripping from the data any serializer fields that do not correspond to a specific model field
Source code in nautobot/core/api/mixins.py
to_internal_value(data)
¶
Return an object or a list of objects based on a dictionary of data attributes or an UUID.
Source code in nautobot/core/api/mixins.py
nautobot.apps.api.dict_to_filter_params(d, prefix='')
¶
Translate a dictionary of attributes to a nested set of parameters suitable for QuerySet filtering. For example:
{
"name": "Foo",
"rack": {
"facility_id": "R101"
}
}
Becomes
{ "name": "Foo", "rack__facility_id": "R101" }
And can be employed as filter parameters
Device.objects.filter(**dict_to_filter(attrs_dict))
Source code in nautobot/core/api/utils.py
nautobot.apps.api.dynamic_import(name)
¶
Dynamically import a class from an absolute path string
nautobot.apps.api.get_api_version_serializer(serializer_choices, api_version)
¶
Returns the serializer of an api_version
Parameters:
Name | Type | Description | Default |
---|---|---|---|
serializer_choices |
tuple
|
list of SerializerVersions |
required |
api_version |
str
|
Request API version |
required |
Returns:
Type | Description |
---|---|
Serializer
|
the serializer for the api_version if found in serializer_choices else None |
Source code in nautobot/core/api/utils.py
nautobot.apps.api.get_serializer_for_model(model, prefix='')
¶
Dynamically resolve and return the appropriate serializer for a model.
Raises:
Type | Description |
---|---|
SerializerNotFound
|
if the requested serializer cannot be located. |
Source code in nautobot/core/api/utils.py
nautobot.apps.api.get_view_name(view, suffix=None)
¶
Derive the view name from its associated model, if it has one. Fall back to DRF's built-in get_view_name
.
Source code in nautobot/core/api/utils.py
nautobot.apps.api.is_api_request(request)
¶
nautobot.apps.api.rest_api_server_error(request, *args, **kwargs)
¶
Handle exceptions and return a useful error message for REST API requests.
Source code in nautobot/core/api/utils.py
nautobot.apps.api.versioned_serializer_selector(obj, serializer_choices, default_serializer)
¶
Returns appropriate serializer class depending on request api_version, and swagger_fake_view
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
ViewSet instance
|
|
required |
serializer_choices |
tuple
|
Tuple of SerializerVersions |
required |
default_serializer |
Serializer
|
Default Serializer class |
required |