Explorar el Código

Closes #10314: Move clone() method from NetBoxModel to CloningMixin

jeremystretch hace 3 años
padre
commit
6a9274a95f

+ 2 - 18
docs/plugins/development/models.md

@@ -49,24 +49,6 @@ class MyModel(NetBoxModel):
     ...
 ```
 
-### The `clone()` Method
-
-!!! info
-    This method was introduced in NetBox v3.3.
-
-The `NetBoxModel` class includes a `clone()` method to be used for gathering attributes which can be used to create a "cloned" instance. This is used primarily for form initialization, e.g. when using the "clone" button in the NetBox UI. By default, this method will replicate any fields listed in the model's `clone_fields` list, if defined.
-
-Plugin models can leverage this method by defining `clone_fields` as a list of field names to be replicated, or override this method to replace or extend its content:
-
-```python
-class MyModel(NetBoxModel):
-
-    def clone(self):
-        attrs = super().clone()
-        attrs['extra-value'] = 123
-        return attrs
-```
-
 ### Enabling Features Individually
 
 If you prefer instead to enable only a subset of these features for a plugin model, NetBox provides a discrete "mix-in" class for each feature. You can subclass each of these individually when defining your model. (Your model will also need to inherit from Django's built-in `Model` class.)
@@ -116,6 +98,8 @@ For more information about database migrations, see the [Django documentation](h
 
 ::: netbox.models.features.ChangeLoggingMixin
 
+::: netbox.models.features.CloningMixin
+
 ::: netbox.models.features.CustomLinksMixin
 
 ::: netbox.models.features.CustomFieldsMixin

+ 5 - 0
docs/release-notes/version-3.4.md

@@ -0,0 +1,5 @@
+# NetBox v3.4
+
+### Plugins API
+
+* [#10314](https://github.com/netbox-community/netbox/issues/10314) - Move `clone()` method from NetBoxModel to CloningMixin

+ 1 - 0
mkdocs.yml

@@ -252,6 +252,7 @@ nav:
         - git Cheat Sheet: 'development/git-cheat-sheet.md'
     - Release Notes:
         - Summary: 'release-notes/index.md'
+        - Version 3.4: 'release-notes/version-3.4.md'
         - Version 3.3: 'release-notes/version-3.3.md'
         - Version 3.2: 'release-notes/version-3.2.md'
         - Version 3.1: 'release-notes/version-3.1.md'

+ 11 - 2
netbox/netbox/models/features.py

@@ -92,8 +92,17 @@ class CloningMixin(models.Model):
 
     def clone(self):
         """
-        Return a dictionary of attributes suitable for creating a copy of the current instance. This is used for pre-
-        populating an object creation form in the UI.
+        Returns a dictionary of attributes suitable for creating a copy of the current instance. This is used for pre-
+        populating an object creation form in the UI. By default, this method will replicate any fields listed in the
+        model's `clone_fields` list (if defined), but it can be overridden to apply custom logic.
+
+        ```python
+        class MyModel(NetBoxModel):
+            def clone(self):
+                attrs = super().clone()
+                attrs['extra-value'] = 123
+                return attrs
+        ```
         """
         attrs = {}