Skip to content

Commit efa416b

Browse files
BlayeeRBBooijLiewes
authored andcommitted
Fix multiput with proxy submodels
1 parent 6a4ef26 commit efa416b

File tree

7 files changed

+49
-2
lines changed

7 files changed

+49
-2
lines changed

binder/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2345,8 +2345,17 @@ def _multi_put_override_superclass(self, objects):
23452345
# Collect overrides
23462346
for (cls, mid), data in objects.items():
23472347
for subcls in getsubclasses(cls):
2348+
# Get remote field of the subclass
2349+
remote_field = subcls._meta.pk.remote_field
2350+
2351+
# In some scenarios with proxy models
2352+
# The remote field may not exist
2353+
# Because proxy models are just pure python wrappers(without its own db table) for other models
2354+
if remote_field is None:
2355+
continue
2356+
23482357
# Get key of field pointing to subclass
2349-
subkey = subcls._meta.pk.remote_field.name
2358+
subkey = remote_field.name
23502359
# Get id of subclass
23512360
subid = data.pop(subkey, None)
23522361
if subid is None:

docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ version: '3'
22

33
services:
44
db:
5-
image: postgres:11.5
5+
image: postgres:12
6+
environment:
7+
- POSTGRES_HOST_AUTH_METHOD=trust
68
binder:
79
build: .
810
command: tail -f /dev/null

tests/test_inheritance.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,24 @@ def test_create_lion_with_animal_and_zoo(self):
122122
zoo = Zoo.objects.get(pk=zoo_id)
123123
zoo_animal_ids = [animal.id for animal in zoo.animals.all()]
124124
self.assertEqual(zoo_animal_ids, [animal_id])
125+
126+
def test_multiput_class_that_has_proxy_subclass(self):
127+
response = self.client.put(
128+
'/animal/',
129+
content_type='application/json',
130+
data=json.dumps({
131+
'data': [{
132+
'id': -1,
133+
'name': 'Kowalsky',
134+
}],
135+
'with': {
136+
'zoo': [{
137+
'id': -3,
138+
'name': 'Artis',
139+
'animals': [-1],
140+
}],
141+
},
142+
}),
143+
)
144+
145+
self.assertEqual(response.status_code, 200)

tests/testapp/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .city import City, CityState, PermanentCity
1717
from .country import Country
1818
from .web_page import WebPage
19+
from .pet import Pet
1920

2021
# This is Postgres-specific
2122
if os.environ.get('BINDER_TEST_MYSQL', '0') != '1':

tests/testapp/models/pet.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from binder.models import BinderModel
2+
3+
from .animal import Animal
4+
5+
class Pet(Animal):
6+
class Meta(BinderModel.Meta):
7+
proxy = True

tests/testapp/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
from .zoo_employee import ZooEmployeeView
2222
from .web_page import WebPageView
2323
from .donor import DonorView
24+
from .pet import PetView

tests/testapp/views/pet.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from binder.views import ModelView
2+
3+
from ..models import Pet
4+
5+
class PetView(ModelView):
6+
model = Pet

0 commit comments

Comments
 (0)