@@ -5,30 +5,26 @@ Django classes to make your models, managers, and querysets serializable, with b
5
5
6
6
## Installation
7
7
8
- ` pip install django-serializable-model `
8
+ ``` shell
9
+ pip install django-serializable-model
10
+ ```
9
11
10
12
It is expected that you already have Django installed
11
13
12
14
### Compatibility
13
15
14
16
_ This was originally used in an older Django 1.5 codebase with Python 2.7._
15
17
16
- <br >
17
-
18
18
Should work with Django 1.5-1.9 with Python 2.7.
19
+ - Likely works with Django 1.10 and 1.11, though not 100% sure that ` ._meta.fields ` usage works the same way in these.
20
+ - Will have some problems with Django 2.0 as the Manager's ` use_for_related_fields ` has been removed.
21
+ - ` 2to3 ` shows that there is nothing to change, so should be compatible with Python 3.x
22
+ - Have not confirmed if this works with earlier versions of Django or Python.
19
23
20
- Likely works with Django 1.10 and 1.11, though not 100% sure that ` ._meta.fields ` usage works the same way in these.
21
-
22
- Will have some problems with Django 2.0 as the Manager's ` use_for_related_fields ` has been removed.
23
-
24
- ` 2to3 ` shows that there is nothing to change, so should be compatible with Python 3.x
25
-
26
- Have not confirmed if this works with earlier versions of Django or Python.
24
+ Please submit a PR or file an issue if you have a compatibility problem or have confirmed compatibility on versions.
27
25
28
26
<br >
29
27
30
- Please submit a PR or file an issue if you have a compatibility problem or have confirmed compatibility on versions.
31
-
32
28
33
29
## Usage
34
30
@@ -177,9 +173,18 @@ print User.objects.prefetch_related('post_set').get(pk=new_user.pk).serialize('p
177
173
178
174
` .serialize ` takes in any number of joins as its ` *args ` and they can be of any depth, using the same ` __ ` syntax as ` prefetch_related ` . This means if your ` Post ` object also had ` Comment ` objects, you could write:
179
175
180
- ` User.objects.prefetch_related('post_set__comment_set').serialize('post_set__comment_set') `
176
+ ``` python
177
+ User.objects.prefetch_related(' post_set__comment_set' ).serialize(' post_set__comment_set' )
178
+ ```
181
179
182
- and get an array of ` Comment ` dictionaries within each ` Post ` dictionary.
180
+ and get an array of ` Comment ` dictionaries within each ` Post ` dictionary. If your ` Post ` object also had ` Like ` objects:
181
+
182
+ ``` python
183
+ joins = [' post_set__comment_set' , ' post_set__like_set' ]
184
+ User.objects.prefetch_related(* joins).serialize(* joins)
185
+ ```
186
+
187
+ <br >
183
188
184
189
185
190
### JSON and APIs
@@ -188,6 +193,8 @@ Since `.serialize` outputs a dictionary, one can turn it into JSON simply by usi
188
193
189
194
If you're building an API, you can use ` JSONResponse ` on the dictionary as well.
190
195
196
+ <br >
197
+
191
198
192
199
## How it works
193
200
0 commit comments