Skip to main content

A simple package to create json response data from a Model.

Project description

JSON Convertible Model is a Django app to replace models.Model and make it possible that models will be able to return JSON-Objects directly without creating an new serializer class object. It will not replace the awesome Django-Restframework package but sometimes it is to much afford for me. Detailed documentation is in the “docs” directory.

Quick start

  1. Add “convertable_model” to your INSTALLED_APPS setting like this:

    INSTALLED_APPS = [
        ...
        'convertable_model',
    ]
  2. Create your own models and choose “JSONConvertibleModel” as class to inherit from:

    class FooModel(JSONConvertibleModel):
        ...

2.1. Define which fields you want to use:

class FooModel(JSONConvertibleModel):
    ...

    json_fields = '__all__'

or

class FooModel(JSONConvertibleModel):
    ...

    json_fields = ('field1', 'field2', )

2.2. Define the JSON-Key’s if you want to give them a different name:

class FooModel(JSONConvertibleModel):
    ...

    @jsonify_me
    def json_aliases(self):
        return {
            'foo1': 'IntegerField',
            'foo2': 'My_name'
        }

2.3 If you want to change the return value of the field:

class FooModel(JSONConvertibleModel):
    ...

    @jsonify_me
    def prefix_foobar2(self):
    # import is the decorator and that the field name is written in "{foo}_{attribute}"
        return self.foobar2.upper()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-json-convertible-models-0.1.1.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page