Repository: inventree/InvenTree

Issue:
Settings do not work in combination with api views
### Please verify that this bug has NOT been raised before.

- [X] I checked and didn't find a similar issue

### Describe the bug*

Using DRF views in plugins result in the following error if used in combination with settings mixin:
```
_pickle.PicklingError: Can't pickle <function TestView at 0x7f57e44a4af0>: it's not the same object as view_test_plg.TestView
```

### Steps to Reproduce

1. Create a plugin with the following code:

<details>
<summary>Code</summary>

```py
from django.conf.urls import url

from rest_framework.views import APIView
from rest_framework import permissions
from rest_framework.response import Response

from plugin import InvenTreePlugin
from plugin.mixins import PanelMixin, UrlsMixin, SettingsMixin, AppMixin


class TestView(APIView):
    permission_classes = [permissions.IsAuthenticated]

    def get(self, request):
        return Response([])


api_urls = [
    url("test/", TestView.as_view(), name="api-test-view")
]


class InvenTreeViewTestPlugin(AppMixin, UrlsMixin, SettingsMixin, InvenTreePlugin):
    AUTHOR = "wolflu05"
    DESCRIPTION = "InvenTree View test plugin"
    VERSION = "1.0.0"

    TITLE = "InvenTree view test Plugin"
    SLUG = "inventree-view-test"
    NAME = "InvenTreeViewTestPlugin"

    def __init__(self, *args, **kwargs):
        self.SETTINGS: dict[str, dict] = {
            "TEST": {
                "name": "ABC",
                "description": "DEF",
            }
        }

        super().__init__(*args, **kwargs)

    def setup_urls(self):
        return api_urls
```

</details>

2. Activate that plugin and try to open the plugins settings page
3. See error

<details>
<summary>Error</summary>

```
Internal Server Error: /settings/
Traceback (most recent call last):
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 204, in _get_response
    response = response.render()
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 214, in render
    nodelist.append(node.render_annotated(context))
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 214, in render
    nodelist.append(node.render_annotated(context))
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/workspaces/InvenTree/InvenTree/part/templatetags/inventree_extras.py", line 357, in setting_object
    return PluginSetting.get_setting_object(key, plugin=plugin, cache=cache)
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 502, in get_setting_object
    setting.save(**kwargs)
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 183, in save
    self.save_to_cache()
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 221, in save_to_cache
    cache.set(
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/cache/backends/locmem.py", line 56, in set
    pickled = pickle.dumps(value, self.pickle_protocol)
_pickle.PicklingError: Can't pickle <function TestView at 0x7f57e44a4af0>: it's not the same object as view_test_plg.TestView
Internal Server Error: /settings/
Traceback (most recent call last):
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 204, in _get_response
    response = response.render()
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 214, in render
    nodelist.append(node.render_annotated(context))
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 214, in render
    nodelist.append(node.render_annotated(context))
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/loader_tags.py", line 195, in render
    return template.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 162, in _render
    return self.nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/template/library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/workspaces/InvenTree/InvenTree/part/templatetags/inventree_extras.py", line 357, in setting_object
    return PluginSetting.get_setting_object(key, plugin=plugin, cache=cache)
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 502, in get_setting_object
    setting.save(**kwargs)
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 183, in save
    self.save_to_cache()
  File "/workspaces/InvenTree/InvenTree/common/models.py", line 221, in save_to_cache
    cache.set(
  File "/workspaces/InvenTree/dev/venv/lib/python3.10/site-packages/django/core/cache/backends/locmem.py", line 56, in set
    pickled = pickle.dumps(value, self.pickle_protocol)
_pickle.PicklingError: Can't pickle <function TestView at 0x7f57e44a4af0>: it's not the same object as view_test_plg.TestView
[07/Aug/2023 21:42:00] "GET /settings/ HTTP/1.1" 500 720101
```

</details>

### Expected behaviour

Should work

### Deployment Method

- [X] Docker
- [X] Bare metal

### Version Information

# Version Information:
InvenTree-Version: 0.13.0 dev
Django Version: 3.2.20
Commit Hash: 4b61a34
Commit Date: 2023-07-27
Commit Branch: pui/server-settings
Database: sqlite3
Debug-Mode: True
Deployed using Docker: False
Platform: Linux-5.15.49-linuxkit-pr-x86_64-with-glibc2.31
Installer: GIT

Active plugins: [{'name': 'InvenTreeBarcode', 'slug': 'inventreebarcode', 'version': '2.0.0'}, {'name': 'InvenTreeCoreNotificationsPlugin', 'slug': 'inventreecorenotificationsplugin', 'version': '1.0.0'}, {'name': 'InvenTreeLabel', 'slug': 'inventreelabel', 'version': '1.0.0'}, {'name': 'InvenTreeViewTestPlugin', 'slug': 'inventree-view-test', 'version': '1.0.0'}]


### Please verify if you can reproduce this bug on the demo site.

- [ ] I can reproduce this bug on the demo site.

### Relevant log output

_No response_


Discussion:

Resolving patch:
diff --git a/InvenTree/plugin/models.py b/InvenTree/plugin/models.py
--- a/InvenTree/plugin/models.py
+++ b/InvenTree/plugin/models.py
@@ -103,6 +103,12 @@ def get_plugin_meta(name):
         # Save plugin
         self.plugin: InvenTreePlugin = plugin
 
+    def __getstate__(self):
+        """Customize pickeling behaviour."""
+        state = super().__getstate__()
+        state.pop("plugin", None)  # plugin cannot be pickelt in some circumstances when used with drf views, remove it (#5408)
+        return state
+
     def save(self, force_insert=False, force_update=False, *args, **kwargs):
         """Extend save method to reload plugins if the 'active' status changes."""
         reload = kwargs.pop('no_reload', False)  # check if no_reload flag is set


Test patch: