Skip to content

Commit

Permalink
Fix multiput with proxy submodels
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayeeR committed May 3, 2024
1 parent 3388534 commit eb46c18
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion binder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/testapp/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
7 changes: 7 additions & 0 deletions tests/testapp/models/pet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from binder.models import BinderModel

from .animal import Animal

class Pet(Animal):
class Meta(BinderModel.Meta):
proxy = True
1 change: 1 addition & 0 deletions tests/testapp/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
from .zoo_employee import ZooEmployeeView
from .web_page import WebPageView
from .donor import DonorView
from .pet import PetView
6 changes: 6 additions & 0 deletions tests/testapp/views/pet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from binder.views import ModelView

from ..models import Pet

class PetView(ModelView):
model = Pet

0 comments on commit eb46c18

Please sign in to comment.