Relating Custom Objects to Each Other
Scenario: you want to track software licenses. Each vendor sells products, products can be bundled into suites (Adobe Creative Cloud includes Photoshop and Acrobat), and each product has licenses. A license key only needs to be unique within its product.
You'll use: fields that reference another Custom Object Type, a field that references its own type, a reverse relation name, and a unique constraint over two fields.
Before You Start
- The vendors exist in NetBox as manufacturers (Devices > Manufacturers), e.g. Adobe and Autodesk.
- Your user can add Custom Object Types, fields and constraints.
Steps
1. Create the Software Product Type
-
Go to Custom Objects > Custom Object Types and click Add.
-
Set Internal name to
software_product, Display name (singular) toSoftware Productand Display name (plural) toSoftware Products, then click Create. -
Add these fields (Add > Field on the type's page):
Name Label Type Display weight Other settings nameName Text 100 Required, Primary name field vendorVendor Object 110 Required; Related object type: DCIM > Manufacturer suiteSuite Object 120 Related object type: Custom Objects > Software Product; Reverse relation name: suite_products
The Suite field points at the type it belongs to, so a product can be part of another product.
2. Create the Software License Type
-
Create a second type with Internal name
software_license, Display name (singular)Software Licenseand Display name (plural)Software Licenses. -
Add these fields:
Name Label Type Display weight Other settings productProduct Object 100 Required; Related object type: Custom Objects > Software Product; Reverse relation name: licenses; On delete behavior: Protectlicense_keyLicense key Text 110 Required seatsSeats Integer 120 Minimum value: 1expiresExpires Date 130
On delete behavior: Protect stops a product from being deleted while it still has licenses.
3. Make License Keys Unique per Product
- On the Software License type's page, click Add and choose Constraint.
- Set Name to
unique_key_per_productand Type to Unique. - Under Fields, choose Product and License key.
- Click Create.
4. Name Licenses Usefully
Licenses have no field that makes a good name on its own. On the Software License type's page, click Edit, set Display expression to {{ product }}: {{ license_key }}, and save.
5. Add the Data
- Add a product Creative Cloud (vendor Adobe), then Photoshop and Acrobat, each with Suite set to Creative Cloud. Create & Add Another keeps the form open between them.
- Add two licenses for Photoshop, e.g. keys
PS-AAAA-1111andPS-BBBB-2222. They're named after the display expression, e.g. Photoshop: PS-AAAA-1111. - Try adding a third Photoshop license with the key
PS-AAAA-1111. Saving it fails with "Software License with this Product and License key already exists." Change its product to Acrobat and save again: the same key is accepted for a different product.
6. Navigate the Hierarchy
- Open Creative Cloud. Its Custom Objects tab lists Photoshop and Acrobat (through Suite).
- Open Photoshop. Its Custom Objects tab lists its licenses.
- Open the Adobe manufacturer. Its Custom Objects tab lists the Adobe products, and its Related Objects panel links to them.
- Try deleting Photoshop. NetBox refuses straight away and lists the licenses that still reference it.
Result
Products, suites and licenses are linked in a hierarchy you can follow in both directions, and license keys are unique within each product.
See Also
- Field Attributes: Object Reference Fields
- Constraints
- GraphQL API, for querying the whole hierarchy in one request.
Promoting a Schema Between Instances
On staging, edit the Support Contract type, set Version to 1.0.0, and save. The version travels with the schema document, so you can tell which revision an i...
A Self-Contained Object Type
1. Go to Customization > Custom Field Choices and click Add. 2. Set Name to Contract status. 3. In Extra choices, enter one choice per line: