Skip to content

Commit 3854fa9

Browse files
committed
fix prefetching
1 parent b24ba1e commit 3854fa9

File tree

5 files changed

+83
-78
lines changed

5 files changed

+83
-78
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.4.1 (2021-04-09)
4+
5+
- Fix sideloadable prefetches
6+
37
## 1.4.0 (2021-04-07)
48

59
- Python supported versions `3.6 -> 3.9`

README.md

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,100 +14,100 @@ DRF-sideloading is an extension to provide side-loading functionality of related
1414

1515
## Quickstart
1616

17-
1. Install drf-sideloading:
17+
1. Install drf-sideloading:
1818

19-
```shell
20-
pip install drf-sideloading
21-
```
19+
```shell
20+
pip install drf-sideloading
21+
```
2222

2323
2. Import `SideloadableRelationsMixin`:
2424

25-
```python
26-
from drf_sideloading.mixins import SideloadableRelationsMixin
27-
```
25+
```python
26+
from drf_sideloading.mixins import SideloadableRelationsMixin
27+
```
2828

2929
3. Write your SideLoadableSerializer
3030
You need to define the **primary** serializer in the Meta data and can define prefetching rules. Also notice the **many=True** on the sideloadable relationships.
3131

32-
```python
33-
from drf_sideloading.serializers import SideLoadableSerializer
34-
35-
class ProductSideloadableSerializer(SideLoadableSerializer):
36-
products = ProductSerializer(many=True)
37-
categories = CategorySerializer(source="category", many=True)
38-
suppliers = SupplierSerializer(source="supplier", many=True)
39-
partners = PartnerSerializer(many=True)
40-
41-
class Meta:
42-
primary = "products"
43-
prefetches = {
44-
"categories": "category",
45-
"suppliers": "supplier",
46-
"partners": "partners",
47-
}
48-
```
32+
```python
33+
from drf_sideloading.serializers import SideLoadableSerializer
34+
35+
class ProductSideloadableSerializer(SideLoadableSerializer):
36+
products = ProductSerializer(many=True)
37+
categories = CategorySerializer(source="category", many=True)
38+
suppliers = SupplierSerializer(source="supplier", many=True)
39+
partners = PartnerSerializer(many=True)
40+
41+
class Meta:
42+
primary = "products"
43+
prefetches = {
44+
"categories": "category",
45+
"suppliers": "supplier",
46+
"partners": "partners",
47+
}
48+
```
4949

5050
4. Configure sideloading
5151
Include **SideloadableRelationsMixin** mixin in ViewSet and define **sideloading_serializer_class** as shown in example below. Evrything else stays just like a regular ViewSet
5252

53-
```python
54-
from drf_sideloading.mixins import SideloadableRelationsMixin
55-
56-
class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
57-
"""
58-
A simple ViewSet for viewing and editing products.
59-
"""
60-
61-
queryset = Product.objects.all()
62-
serializer_class = ProductSerializer
63-
sideloading_serializer_class = ProductSideloadableSerializer
64-
```
53+
```python
54+
from drf_sideloading.mixins import SideloadableRelationsMixin
55+
56+
class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
57+
"""
58+
A simple ViewSet for viewing and editing products.
59+
"""
60+
61+
queryset = Product.objects.all()
62+
serializer_class = ProductSerializer
63+
sideloading_serializer_class = ProductSideloadableSerializer
64+
```
6565

6666
5. Enjoy your API with sideloading support
6767

68-
```http
69-
GET /api/products/?sideload=categories,partners,suppliers,products
70-
```
68+
```http
69+
GET /api/products/?sideload=categories,partners,suppliers,products
70+
```
7171

72-
```json
73-
{
74-
"products": [
75-
{
76-
"id": 1,
77-
"name": "Product 1",
78-
"category": 1,
79-
"supplier": 1,
80-
"partners": [1, 2, 3]
81-
}
82-
],
83-
"categories": [
72+
```json
8473
{
85-
"id": 1,
86-
"name": "Category1"
87-
}
88-
],
89-
"suppliers": [
90-
{
91-
"id": 1,
92-
"name": "Supplier1"
93-
}
94-
],
95-
"partners": [
96-
{
97-
"id": 1,
98-
"name": "Partner1"
99-
},
100-
{
101-
"id": 2,
102-
"name": "Partner1"
103-
},
104-
{
105-
"id": 3,
106-
"name": "Partner3"
74+
"products": [
75+
{
76+
"id": 1,
77+
"name": "Product 1",
78+
"category": 1,
79+
"supplier": 1,
80+
"partners": [1, 2, 3]
81+
}
82+
],
83+
"categories": [
84+
{
85+
"id": 1,
86+
"name": "Category1"
87+
}
88+
],
89+
"suppliers": [
90+
{
91+
"id": 1,
92+
"name": "Supplier1"
93+
}
94+
],
95+
"partners": [
96+
{
97+
"id": 1,
98+
"name": "Partner1"
99+
},
100+
{
101+
"id": 2,
102+
"name": "Partner1"
103+
},
104+
{
105+
"id": 3,
106+
"name": "Partner3"
107+
}
108+
]
107109
}
108-
]
109-
}
110-
```
110+
```
111111

112112
## Example Project
113113

drf_sideloading/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.4.0"
1+
__version__ = "1.4.1"

drf_sideloading/mixins.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def get_sideloading_prefetches(self):
8080
for vi in v:
8181
if not isinstance(vi, (str, Prefetch)):
8282
raise RuntimeError("Sideloadable prefetch values must be a list of strings or Prefetch objects")
83+
cleaned_prefetches[k] = v
8384
return cleaned_prefetches
8485

8586
def initialize_request(self, request, *args, **kwargs):

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 1.4.0
2+
current_version = 1.4.1
33
commit = True
44
tag = True
55

0 commit comments

Comments
 (0)