Repository: wagtail/wagtail

Issue:
Exception when custom site settings haven't been created yet 4.0rc1
### Issue Summary

This issue seems to be related to the issue #8996, with the difference that if the settings haven't been initialized/created in the db, this error occurs:
```
Internal Server Error: /admin/settings/home/testsetting/2/
Traceback (most recent call last):
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/query.py", line 928, in get_or_create
    return self.get(**kwargs), False
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/query.py", line 650, in get
    raise self.model.DoesNotExist(
home.models.TestSetting.DoesNotExist: TestSetting matching query does not exist.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2018, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'TestSetting'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/views/decorators/cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/wagtail/admin/urls/__init__.py", line 165, in wrapper
    return view_func(request, *args, **kwargs)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/wagtail/admin/auth.py", line 205, in decorated_view
    return view_func(request, *args, **kwargs)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/wagtail/contrib/settings/views.py", line 101, in edit
    instance = model.for_site(site)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/wagtail/contrib/settings/models.py", line 143, in for_site
    instance, created = queryset.get_or_create(site=site)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/query.py", line 935, in get_or_create
    return self.create(**params), True
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/query.py", line 671, in create
    obj.save(force_insert=True, using=self.db)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/base.py", line 831, in save
    self.save_base(
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/base.py", line 882, in save_base
    updated = self._save_table(
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1025, in _save_table
    results = self._do_insert(
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/base.py", line 1066, in _do_insert
    return manager._insert(
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1790, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1656, in execute_sql
    for sql, params in self.as_sql():
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1580, in as_sql
    value_rows = [
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1581, in <listcomp>
    [
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1582, in <listcomp>
    self.prepare_value(field, self.pre_save_val(field, obj))
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1520, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 925, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2703, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/***/wagtail-4.0-test/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2020, in get_prep_value
    raise e.__class__(
TypeError: Field 'id' expected a number but got <TestSetting: Test setting for localhost [default]>.
```

### Steps to Reproduce

1. Follow https://github.com/wagtail/wagtail/issues/8996#issuecomment-1215740647, BUT directly install and start with wagtail 4.0.
2. When you reach `6. Go to Setting > Test Settings. Everything works.` of the above guide, you will see the above error.

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

-   I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes

### Technical details

-   Python version: 3.10
-   Django version: 4.1
-   Wagtail version: 4.0rc1 (tested with nightly https://releases.wagtail.org/nightly/dist/wagtail-4.0.dev20220816-py3-none-any.whl)



Discussion:
It looks like it has something (magic) to do with the deprecated `wagtail.contrib.settings.models.BaseSetting` class, which is normally used when upgrading an existing wagtail<4 installation.
Changing your custom settings model to inherit from `wagtail.contrib.settings.models.BaseSiteSetting` instead of `wagtail.contrib.settings.models.BaseSetting` fixes this and also #8996 for me.

This change is also documented in the [release notes for 4.0](https://docs.wagtail.org/en/latest/releases/4.0.html#basesetting-model-replaced-by-basesitesetting), but I think it was intended to work in a backwards compatible way...


Resolving patch:
diff --git a/wagtail/contrib/settings/models.py b/wagtail/contrib/settings/models.py
--- a/wagtail/contrib/settings/models.py
+++ b/wagtail/contrib/settings/models.py
@@ -217,4 +217,4 @@ def __init__(self, *args, **kwargs):
             category=RemovedInWagtail50Warning,
             stacklevel=2,
         )
-        return super().__init__(self, *args, **kwargs)
+        super().__init__(*args, **kwargs)


Test patch: