nautobot.apps.api
¶
Helpers for an app to implement a REST API.
nautobot.apps.api.BaseModelSerializer
¶
Bases: OptInFieldsMixin
, serializers.ModelSerializer
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.
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 ensure certain fields are present even when not explicitly stated in Meta.fields.
DRF does not automatically add declared fields to Meta.fields
, nor does it require that declared fields
on a super class be included in Meta.fields
to allow for a subclass to include only a subset of declared
fields from the super. This means either we intercept and ensure the fields at this level, or
enforce by convention that all consumers of BaseModelSerializer include each of these standard fields in their
Meta.fields
which would surely lead to errors of omission; therefore we have chosen the former approach.
Adds "id" and "display" to the start of fields
for all models; also appends "created" and "last_updated"
to the end of fields
if they are applicable to this model and this is not a Nested serializer.
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/extras/api/customfields.py
get_field_names(declared_fields, info)
¶
Ensure that "custom_fields" and "computed_fields" are always included appropriately.
Source code in nautobot/extras/api/customfields.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 and relationships.
Source code in nautobot/extras/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/extras/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that fields includes "notes_url" field if applicable.
Source code in nautobot/extras/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/extras/api/relationships.py
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 |
|
get_field_names(declared_fields, info)
¶
Ensure that "relationships" is always included as an opt-in field.
Source code in nautobot/extras/api/relationships.py
nautobot.apps.api.StatusModelSerializerMixin
¶
Bases: BaseModelSerializer
Mixin to add status
choice field to model serializers.
Source code in nautobot/extras/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that "status" field is always present.
status_choices()
¶
Get the list of valid status values for this serializer.
In the case where multiple serializers have the same set of status choices, it's necessary to set settings.SPECTACULAR_SETTINGS["ENUM_NAME_OVERRIDES"] for at least one of the matching serializers, or else drf-spectacular will report: 'enum naming encountered a non-optimally resolvable collision for fields named "status"'
Source code in nautobot/extras/api/serializers.py
nautobot.apps.api.TaggedModelSerializerMixin
¶
Bases: BaseModelSerializer
Source code in nautobot/extras/api/serializers.py
get_field_names(declared_fields, info)
¶
Ensure that 'tags' field is always present.
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.