nautobot.apps.models
¶
Data model classes and utilities for app implementation.
nautobot.apps.models.BaseModel
¶
Bases: models.Model
Base model class that all models should inherit from.
This abstract base provides globally common fields and functionality.
Here we define the primary key to be a UUID field and set its default to
automatically generate a random UUID value. Note however, this does not
operate in the same way as a traditional auto incrementing field for which
the value is issued by the database upon initial insert. In the case of
the UUID field, Django creates the value upon object instantiation. This
means the canonical pattern in Django of checking self.pk is None
to tell
if an object has been created in the actual database does not work because
the object will always have the value populated prior to being saved to the
database for the first time. An alternate pattern of checking not self.present_in_database
can be used for the same purpose in most cases.
Source code in nautobot/core/models/__init__.py
13 14 15 16 17 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 |
|
natural_key_slug: str
property
¶
Automatic "slug" string derived from this model's natural key, suitable for use in URLs etc.
A less naïve implementation than django-natural-keys provides by default, based around URL percent-encoding.
present_in_database
property
¶
True if the record exists in the database, False if it does not.
get_absolute_url(api=False)
¶
Return the canonical URL for this object in either the UI or the REST API.
Source code in nautobot/core/models/__init__.py
natural_key()
¶
Smarter default implementation of natural key construction.
- Handles nullable foreign keys (https://github.com/wq/django-natural-keys/issues/18)
- Handles variadic natural-keys (e.g. Location model - [name, parent__name, parent__parent__name, ...].)
Source code in nautobot/core/models/__init__.py
natural_key_args_to_kwargs(args)
classmethod
¶
Helper function to map a list of natural key field values to actual kwargs suitable for lookup and filtering.
Based on django-natural-keys
NaturalKeyQuerySet.natural_key_kwargs()
method.
Source code in nautobot/core/models/__init__.py
natural_key_field_lookups()
¶
List of lookups (possibly including nested lookups for related models) that make up this model's natural key.
BaseModel provides a "smart" implementation that tries to determine this automatically,
but you can also explicitly set natural_key_field_names
on a given model subclass if desired.
This property is based on a consolidation of django-natural-keys
ForeignKeyModel.get_natural_key_info()
,
ForeignKeyModel.get_natural_key_def()
, and ForeignKeyModel.get_natural_key_fields()
.
Unlike get_natural_key_def()
, this doesn't auto-exclude all AutoField and BigAutoField fields,
but instead explicitly discounts the id
field (only) as a candidate.
Source code in nautobot/core/models/__init__.py
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 |
|
validated_save()
¶
Perform model validation during instance save.
This is a convenience method that first calls self.full_clean()
and then self.save()
which in effect enforces model validation prior to saving the instance, without having
to manually make these calls seperately. This is a slight departure from Django norms,
but is intended to offer an optional, simplified interface for performing this common
workflow. The intended use is for user defined Jobs run via the nautobot-server nbshell
command.
Source code in nautobot/core/models/__init__.py
nautobot.apps.models.CustomValidator
¶
This class is used to register plugin custom model validators which act on specified models. It contains the clean
method which is overridden by plugin authors to execute custom validation logic. Plugin authors must raise
ValidationError within this method to trigger validation error messages which are propagated to the user.
A convenience method validation_error(<message>)
may be used for this purpose.
The model
attribute on the class defines the model to which this validator is registered. It
should be set as a string in the form <app_label>.<model_name>
.
Source code in nautobot/extras/plugins/__init__.py
clean()
¶
Implement custom model validation in the standard Django clean method pattern. The model instance is accessed
with the object
key within self.context
, e.g. self.context['object']
. ValidationError must be raised to
prevent saving model instance changes, and propagate messages to the user. For convenience,
self.validation_error(<message>)
may be called to raise a ValidationError.
Source code in nautobot/extras/plugins/__init__.py
validation_error(message)
¶
Convenience method for raising django.core.exceptions.ValidationError
which is required in order to
trigger validation error messages which are propagated to the user.
Source code in nautobot/extras/plugins/__init__.py
nautobot.apps.models.OrganizationalModel
¶
Bases: BaseModel
, ChangeLoggedModel
, CustomFieldModel
, RelationshipModel
, DynamicGroupMixin
, NotesMixin
Base abstract model for all organizational models.
Organizational models aid the primary models by building structured relationships and logical groups, or categorizations. Organizational models do not typically represent concrete networking resources or assets, but rather they enable user specific use cases and metadata about network resources. Examples include Device Role, Rack Group, Status, Manufacturer, and Platform.
Source code in nautobot/core/models/generics.py
nautobot.apps.models.PrimaryModel
¶
Bases: BaseModel
, ChangeLoggedModel
, CustomFieldModel
, RelationshipModel
, DynamicGroupMixin
, NotesMixin
Base abstract model for all primary models.
A primary model is one which is materialistically relevant to the network datamodel. Such models form the basis of major elements of the data model, like Device, IP Address, Site, VLAN, Virtual Machine, etc. Primary models usually represent tangible or logical resources on the network, or within the organization.
Source code in nautobot/core/models/generics.py
nautobot.apps.models.StatusField
¶
Bases: ForeignKeyLimitedByContentTypes
Model database field that automatically limits custom choices.
The limit_choices_to for the field are automatically derived from
- the content-type to which the field is attached (e.g.
dcim.device
)
Source code in nautobot/extras/models/statuses.py
contribute_to_class(cls, *args, **kwargs)
¶
Overload default so that we can assert that .get_FOO_display
is
attached to any model that is using a StatusField
.
Using .contribute_to_class()
is how field objects get added to the model
at during the instance preparation. This is also where any custom model
methods are hooked in. So in short this method asserts that any time a
StatusField
is added to a model, that model also gets a
.get_status_display()
and a .get_status_color()
method without
having to define it on the model yourself.
Source code in nautobot/extras/models/statuses.py
nautobot.apps.models.VarbinaryIPField
¶
Bases: models.BinaryField
IP network address
Source code in nautobot/ipam/fields.py
db_type(connection)
¶
Returns the correct field type for a given database vendor.
Source code in nautobot/ipam/fields.py
from_db_value(value, expression, connection)
¶
get_db_prep_value(value, connection, prepared=False)
¶
Converts Python (str) to DB (varbinary).
Source code in nautobot/ipam/fields.py
to_python(value)
¶
value_to_string(obj)
¶
nautobot.apps.models.extras_features(*features)
¶
Decorator used to register extras provided features to a model