Models within each app are stored in either models.py or within a submodule under the models/ directory. When creating a model, be sure to subclass the appropriate base model from netbox.models. This will typically be PrimaryModel or OrganizationalModel. Remember to add the model class to the __all__ listing for the module.
Each model should define, at a minimum:
Meta class specifying a deterministic ordering (if ordered by fields other than the primary ID)__str__() method returning a user-friendly string representation of the instanceget_absolute_url() method returning an instance's direct URL (using reverse())If the model has one or more fields with static choices, define those choices in choices.py by subclassing utilities.choices.ChoiceSet.
Once your model definition is complete, generate database migrations by running manage.py makemigrations -n $NAME --no-header. Always specify a short unique name when generating migrations.
!!! info "Configuration Required"
Set `DEVELOPER = True` in your NetBox configuration to enable the creation of new migrations.
Most models will need view classes created in views.py to serve the following operations:
Add the relevant URL path for each view created in the previous step to urls.py.
Depending on the type of model being added, you may need to define several types of form classes. These include:
Each model should have a corresponding FilterSet class defined. This is used to filter UI and API queries. Subclass the appropriate class from netbox.filtersets that matches the model's parent class.
Create a table class for the model in tables.py by subclassing utilities.tables.BaseTable. Under the table's Meta class, be sure to list both the fields and default columns.
Create the HTML template for the object view. (The other views each typically employ a generic template.) This template should extend generic/object.html.
Add the relevant navigation menu items in netbox/netbox/navigation_menu.py.
Create the following for each model:
api/serializers.pyapi/nested_serializers.pyapi/views.pyapi/urls.pyCreate a Graphene object type for the model in graphql/types.py by subclassing the appropriate class from netbox.graphql.types.
Also extend the schema class defined in graphql/schema.py with the individual object and object list fields per the established convention.
Add tests for the following:
Create a new documentation page for the model in docs/models/<app_label>/<model_name>.md. Include this file under the "features" documentation where appropriate.
Also add your model to the index in docs/development/models.md.