You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -14,7 +14,7 @@ DRF-sideloading is an extension to provide side-loading functionality of related
14
14
15
15
## Quickstart
16
16
17
-
1.Install drf-sideloading:
17
+
1. Install drf-sideloading:
18
18
19
19
```shell
20
20
pip install drf-sideloading
@@ -26,29 +26,107 @@ DRF-sideloading is an extension to provide side-loading functionality of related
26
26
from drf_sideloading.mixins import SideloadableRelationsMixin
27
27
```
28
28
29
-
3. Write your SideLoadableSerializer
30
-
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.
29
+
3. Write your SideLoadableSerializer:
30
+
31
+
You need to define the **primary** serializer in the Meta data and can define prefetching rules.
32
+
Also notice the **many=True** on the sideloadable relationships.
31
33
32
34
```python
33
35
from drf_sideloading.serializers import SideLoadableSerializer
34
36
35
37
class ProductSideloadableSerializer(SideLoadableSerializer):
Include **SideloadableRelationsMixin** mixin in ViewSet and define **sideloading_serializer_class** as shown in example below. Evrything else stays just like a regular ViewSet
58
+
59
+
4. Prefetches
60
+
61
+
For fields where the source is provided or where the source matches the field name, prefetches are not strictly required
62
+
63
+
Multiple prefetches can be added to a single sideloadable field, but when using Prefetch object check that they don't clash with prefetches made in the get_queryset() method
Multiple sources can be added to a field using a dict.
82
+
Each key is a source_key that can be used to filter what sources should be sideloaded.
83
+
The values set the source and prefetches for this source.
84
+
85
+
Note that this prefetch reuses `primary_supplier` and `secondary_suppliers` if suppliers and primary_supplier or secondary_suppliers are sideloaded
86
+
```python
87
+
prefetches = {
88
+
"primary_suppliers": "primary_supplier",
89
+
"secondary_suppliers": "secondary_suppliers",
90
+
"suppliers": {
91
+
"primary_suppliers": "primary_supplier",
92
+
"secondary_suppliers": "secondary_suppliers"
93
+
}
94
+
}
95
+
```
96
+
97
+
Usage of Prefetch() objects is supported.
98
+
Prefetch() objects can be used to filter a subset of some relations or just to prefetch or select complicated related objects
99
+
In case there are prefetch conflicts, `to_attr` can be set but be aware that this prefetch will now be a duplicate of similar prefetches.
100
+
prefetch conflicts can also come from prefetched made in the ViewSet.get_queryset() method.
101
+
102
+
Note that this prefetch noes not reuse `primary_supplier` and `secondary_suppliers` if **suppliers** and **primary_supplier** or **secondary_suppliers** are sideloaded at the same time.
Example request and response when fetching all possible values
68
165
```http
69
-
GET /api/products/?sideload=categories,partners,suppliers,products
166
+
GET /api/products/?sideload=categories,partners,primary_suppliers,secondary_suppliers,suppliers,products
70
167
```
71
168
72
169
```json
@@ -76,7 +173,8 @@ DRF-sideloading is an extension to provide side-loading functionality of related
76
173
"id": 1,
77
174
"name": "Product 1",
78
175
"category": 1,
79
-
"supplier": 1,
176
+
"primary_supplier": 1,
177
+
"secondary_suppliers": [2, 3],
80
178
"partners": [1, 2, 3]
81
179
}
82
180
],
@@ -86,10 +184,34 @@ DRF-sideloading is an extension to provide side-loading functionality of related
86
184
"name": "Category1"
87
185
}
88
186
],
187
+
"primary_suppliers": [
188
+
{
189
+
"id": 1,
190
+
"name": "Supplier1"
191
+
}
192
+
],
193
+
"secondary_suppliers": [
194
+
{
195
+
"id": 2,
196
+
"name": "Supplier2"
197
+
},
198
+
{
199
+
"id": 3,
200
+
"name": "Supplier3"
201
+
}
202
+
],
89
203
"suppliers": [
90
204
{
91
205
"id": 1,
92
206
"name": "Supplier1"
207
+
},
208
+
{
209
+
"id": 2,
210
+
"name": "Supplier2"
211
+
},
212
+
{
213
+
"id": 3,
214
+
"name": "Supplier3"
93
215
}
94
216
],
95
217
"partners": [
@@ -108,7 +230,35 @@ DRF-sideloading is an extension to provide side-loading functionality of related
108
230
]
109
231
}
110
232
```
233
+
234
+
The user can also select what sources to load to Multi source fields.
235
+
Leaving the selections empty or omitting the brackets will load all the prefetched sources.
236
+
237
+
Example:
111
238
239
+
```http
240
+
GET /api/products/?sideload=suppliers[primary_suppliers]
241
+
```
242
+
```json
243
+
{
244
+
"products": [
245
+
{
246
+
"id": 1,
247
+
"name": "Product 1",
248
+
"category": 1,
249
+
"primary_supplier": 1,
250
+
"secondary_suppliers": [2, 3],
251
+
"partners": [1, 2, 3]
252
+
}
253
+
],
254
+
"suppliers": [
255
+
{
256
+
"id": 1,
257
+
"name": "Supplier1"
258
+
}
259
+
]
260
+
}
261
+
```
112
262
## Example Project
113
263
114
264
Directory `example` contains an example project using django rest framework sideloading library. You can set it up and run it locally using following commands:
0 commit comments