From eb46c18180e5ed4cf64432ed32cbef1563a7d6e3 Mon Sep 17 00:00:00 2001 From: Jakub Olech Date: Fri, 3 May 2024 17:44:12 +0200 Subject: [PATCH 1/2] Fix multiput with proxy submodels --- binder/views.py | 11 ++++++++++- docker-compose.yml | 4 +++- tests/test_inheritance.py | 21 +++++++++++++++++++++ tests/testapp/models/__init__.py | 1 + tests/testapp/models/pet.py | 7 +++++++ tests/testapp/views/__init__.py | 1 + tests/testapp/views/pet.py | 6 ++++++ 7 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/testapp/models/pet.py create mode 100644 tests/testapp/views/pet.py diff --git a/binder/views.py b/binder/views.py index b8166326..332b4829 100644 --- a/binder/views.py +++ b/binder/views.py @@ -2325,8 +2325,17 @@ def _multi_put_override_superclass(self, objects): # Collect overrides for (cls, mid), data in objects.items(): for subcls in getsubclasses(cls): + # Get remote field of the subclass + remote_field = subcls._meta.pk.remote_field + + # In some scenarios with proxy models + # The remote field may not exist + # Because proxy models are just pure python wrappers(without its own db table) for other models + if remote_field is None: + continue + # Get key of field pointing to subclass - subkey = subcls._meta.pk.remote_field.name + subkey = remote_field.name # Get id of subclass subid = data.pop(subkey, None) if subid is None: diff --git a/docker-compose.yml b/docker-compose.yml index 5e890a8a..06e132d8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,9 @@ version: '3' services: db: - image: postgres:11.5 + image: postgres:12 + environment: + - POSTGRES_HOST_AUTH_METHOD=trust binder: build: . command: tail -f /dev/null diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index 6cc8cf15..7deb1cd2 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -122,3 +122,24 @@ def test_create_lion_with_animal_and_zoo(self): zoo = Zoo.objects.get(pk=zoo_id) zoo_animal_ids = [animal.id for animal in zoo.animals.all()] self.assertEqual(zoo_animal_ids, [animal_id]) + + def test_multiput_class_that_has_proxy_subclass(self): + response = self.client.put( + '/animal/', + content_type='application/json', + data=json.dumps({ + 'data': [{ + 'id': -1, + 'name': 'Kowalsky', + }], + 'with': { + 'zoo': [{ + 'id': -3, + 'name': 'Artis', + 'animals': [-1], + }], + }, + }), + ) + + self.assertEqual(response.status_code, 200) diff --git a/tests/testapp/models/__init__.py b/tests/testapp/models/__init__.py index 93af8bc6..f03ff3bb 100644 --- a/tests/testapp/models/__init__.py +++ b/tests/testapp/models/__init__.py @@ -16,6 +16,7 @@ from .city import City, CityState, PermanentCity from .country import Country from .web_page import WebPage +from .pet import Pet # This is Postgres-specific if os.environ.get('BINDER_TEST_MYSQL', '0') != '1': diff --git a/tests/testapp/models/pet.py b/tests/testapp/models/pet.py new file mode 100644 index 00000000..48b3b0bf --- /dev/null +++ b/tests/testapp/models/pet.py @@ -0,0 +1,7 @@ +from binder.models import BinderModel + +from .animal import Animal + +class Pet(Animal): + class Meta(BinderModel.Meta): + proxy = True diff --git a/tests/testapp/views/__init__.py b/tests/testapp/views/__init__.py index 000623a2..31fb1049 100644 --- a/tests/testapp/views/__init__.py +++ b/tests/testapp/views/__init__.py @@ -21,3 +21,4 @@ from .zoo_employee import ZooEmployeeView from .web_page import WebPageView from .donor import DonorView +from .pet import PetView diff --git a/tests/testapp/views/pet.py b/tests/testapp/views/pet.py new file mode 100644 index 00000000..d55ac301 --- /dev/null +++ b/tests/testapp/views/pet.py @@ -0,0 +1,6 @@ +from binder.views import ModelView + +from ..models import Pet + +class PetView(ModelView): + model = Pet From c58f0e420d84048c9ea8dea570d591e01e0a6326 Mon Sep 17 00:00:00 2001 From: Bob Booij-Liewes Date: Tue, 25 Jun 2024 15:41:12 +0200 Subject: [PATCH 2/2] Make local testing use django 3. (#242) Local testing uses a different package install process then the CI. To prevent us running postgres 12 default while local testing bu use 11 instead we must also use django 3. To enable this we in the setup.py which is used for local testing require a django 3 version instead of a django 3 or 4 version. Since django 4 is only postgres 12+ compatible. Co-authored-by: Bob Booij-Liewes --- docker-compose.yml | 6 +----- setup.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 06e132d8..b0aeb0c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,10 +1,6 @@ -version: '3' - services: db: - image: postgres:12 - environment: - - POSTGRES_HOST_AUTH_METHOD=trust + image: postgres:11.5 binder: build: . command: tail -f /dev/null diff --git a/setup.py b/setup.py index b2174ccc..a7f8936b 100755 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ 'Topic :: Internet :: WWW/HTTP :: Dynamic Content', ], install_requires=[ - 'Django >= 3.0, < 5.0', + 'Django >= 3.0, < 4.0', 'Pillow >= 3.2.0', 'django-request-id >= 1.0.0', 'requests >= 2.13.0',