Implemented corrections

This commit is contained in:
8ctorres 2024-07-06 16:56:51 +02:00
parent d195c48cd7
commit b4f38e2717
2 changed files with 6 additions and 15 deletions

View File

@ -105,10 +105,8 @@ This approach can span multiple levels of relations. For example, the following
>>> IPAddress.objects.filter(interface__device__site__region__slug="north-america")
```
The `get()` method can be used in place of the `filter()` method if we expect this query to return only one object. This method will yield the actual object resulting from the query, instead of a QuerySet. If the query returns more than one result, it will raise an exception.
!!! note
While the above query is functional, it's not very efficient. There are ways to optimize such requests, however they are out of scope for this document. For more information, see the [Django queryset method reference](https://docs.djangoproject.com/en/stable/ref/models/querysets/) documentation.
There are other methods apart from the `filter()`, such as `get()`, and there are ways to make more optimized and complex requests, but those are out of scope for this document. For more information, see the [Django queryset method reference](https://docs.djangoproject.com/en/stable/ref/models/querysets/) documentation.
Reverse relationships can be traversed as well. For example, the following will find all devices with an interface named "em0":
@ -145,10 +143,10 @@ To return the inverse of a filtered queryset, use `exclude()` instead of `filter
346
```
Queries can all also be executed from a particular object instead of from the model itself. For instance, to get all circuits that are assigned to one site, it is easier to filter from the site itself, instead of using the "Circuit" model and building the query from there. This is particularly useful for configuration templates and export templates, since it allows to query other database objects that are related to the object that we're rendering the template for. The same methods (all, filter, exclude, get...) can be used in this kind of queries.
!!! info
The examples above are intended only to provide a cursory introduction to queryset filtering. For an exhaustive list of the available filters, please consult the [Django queryset API documentation](https://docs.djangoproject.com/en/stable/ref/models/querysets/).
Queries can all also be executed from a particular object instead of from the model itself. This can be particularly useful for configuration templates and export templates.
The examples above are intended only to provide a cursory introduction to queryset filtering. For an exhaustive list of the available filters and methods, please consult the [Django queryset API documentation](https://docs.djangoproject.com/en/stable/ref/models/querysets/).
## Creating and Updating Objects

View File

@ -65,18 +65,11 @@ class AnotherCustomScript(Script):
script_order = (MyCustomScript, AnotherCustomScript)
```
The `run()` method is the entrypoint for the script, and it runs in the context of NetBox's own execution environment. This means from here, everything inside NetBox itself is accesible. The [NetBox Shell](../administration/netbox-shell.md) is a good resource to keep in hand, since it allows to see the objects in NetBox in the same way the script does.
!!! info
The [NetBox Shell](../administration/netbox-shell.md) is a good resource to keep in hand, since it allows to see the objects in NetBox in the same way the script does.
The `run()` method can itself call other methods that are in the same module but outside the "MyCustomScript" class, and if there are several scripts in the same module (this is, in the same Python file), both scripts can reuse the same auxiliary methods, keeping the code cleaner. For this reason, it is encouraged to keep similar scripts in the same module. Script can return a string, which will be displayed in a text box in the web interface after the script finishes. This is useful, for instance, for returning a piece of configuration or information that you want the user to be able to easily copy and paste somewhere else.
## Module Attributes
### `name`
You can define `name` within a script module (the Python file which contains one or more scripts) to set the module name. If `name` is not defined, the module's file name will be used.
## Script Attributes
Script attributes are defined under a class named `Meta` within the script. These are optional, but encouraged.