nautobot.apps.api
¶
Helpers for an app to implement a REST API.
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
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 |
|
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
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_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.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.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.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: CustomFieldModelViewSet
, NotesViewSetMixin
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
notes(request, pk=None)
¶
API methods for returning or creating notes on an object.
Source code in nautobot/extras/api/views.py
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
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 |
|
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.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.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.