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 380 |
|
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.
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.
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
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 |
|
get_field_names(declared_fields, info)
¶
Ensure that the "object_type" field is always included in self.fields.