A simple Django app to covert standard django-models to json
Project description
# dantejsoncoder
dantejsoncoder is a simple Django app to covert standard django-models to json
Quick start
-----------
0. Install::
`pip install django-dantejsoncoder`
1. Add "dantejsoncoder" to your INSTALLED_APPS setting like this::
```
INSTALLED_APPS = (
...
'dantejsoncoder',
)
```
2. Import::
`from dantejsoncoder.coder import DanteEncoder`
3. Simple way (all fields)::
"""get QuerySet"""
`objects = YourModel.objects.all()`
"""return json result"""
`return JsonResponse(objects, encoder=DanteEncoder)`
4. If you need concrete fields in your model class add::
`__to_json_fields__ = ('field1','field2',...)`
5. If you need a special translation, add `__to_json_dict__` method in your model class::
`def __to_json_dict__(self):
return {'field_name': self.val. ... }`
6. If you want to add something in the Encoder, use inheritance::
```
class VirgilEncoder(DanteEncoder):
def default(self, obj):
if isinstance(obj, QuerySet):
result = {}
...
return result
else:
return super().default(obj)
```
dantejsoncoder is a simple Django app to covert standard django-models to json
Quick start
-----------
0. Install::
`pip install django-dantejsoncoder`
1. Add "dantejsoncoder" to your INSTALLED_APPS setting like this::
```
INSTALLED_APPS = (
...
'dantejsoncoder',
)
```
2. Import::
`from dantejsoncoder.coder import DanteEncoder`
3. Simple way (all fields)::
"""get QuerySet"""
`objects = YourModel.objects.all()`
"""return json result"""
`return JsonResponse(objects, encoder=DanteEncoder)`
4. If you need concrete fields in your model class add::
`__to_json_fields__ = ('field1','field2',...)`
5. If you need a special translation, add `__to_json_dict__` method in your model class::
`def __to_json_dict__(self):
return {'field_name': self.val. ... }`
6. If you want to add something in the Encoder, use inheritance::
```
class VirgilEncoder(DanteEncoder):
def default(self, obj):
if isinstance(obj, QuerySet):
result = {}
...
return result
else:
return super().default(obj)
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-dantejsoncoder-0.1.tar.gz
(15.3 kB
view hashes)
Close
Hashes for django-dantejsoncoder-0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88acc077344694ef233ae5f5556228f80f27dbd536ad4802c7d8b9de72ee35d4 |
|
MD5 | 3f0a033919a9299c3abeae3ecc07e3d5 |
|
BLAKE2b-256 | 59e814b14d8cf6c84b5ccfa2bccc7f5c070594f705fc976a4e509f04a7710463 |