Skip to main content

A Django fake ORM model that query an RestAPI instead of a database.

Project description

Allow to query a django RestAPI with same interface as the django ORM. (the targeted API must use django-rest-framework + dynamic-rest libraries) In fact, it works like any other database engine. You add the rest_models engine in an alternate database and the rest_models databe router. Then add APIMeta class to the models querying the API, voilà !

Stable branch

https://img.shields.io/travis/Yupeek/django-rest-models/master.svg https://readthedocs.org/projects/django-rest-models/badge/?version=latest https://coveralls.io/repos/github/Yupeek/django-rest-models/badge.svg?branch=master Latest PyPI version Requirements Status

Development status

https://img.shields.io/travis/Yupeek/django-rest-models/develop.svg https://coveralls.io/repos/github/Yupeek/django-rest-models/badge.svg?branch=develop Requirements Status

Installation

  1. Install using pip:

    pip install django-rest-models

  2. Alternatively, you can download or clone this repo and install with :

    pip install -e ..

Requirements

This database wrapper work with

  • python 3.9, 3.10, 3.11, 3.12

  • django 4.2, 5.0, 5.1

On the api, this is tested against

  • django-rest-framework 3.14, 3.15

  • dynamic-rest-bse 2.4 (It’s a fork because dynamic-rest is not compatible with django 4.2 at this day)

Examples

settings.py:

DATABASES = {
    'default': {
        ...
    },
    'api': {
        'ENGINE': 'rest_models.backend',
        'NAME': 'https://requestb.in/',
        'USER': 'userapi',
        'PASSWORD': 'passwordapi',
        'AUTH': 'rest_models.backend.auth.BasicAuth',
    },
}

DATABASE_ROUTERS = [
    'rest_models.router.RestModelRouter',
]

models.py:

class MyModel(models.Model):
    field = models.IntegerField()
    ...

    class Meta:
        # basic django meta Stuff
        verbose_name = 'my model'

    # the only customisation that make this model special
    class APIMeta:
        pass


class MyOtherModel(models.Model):
    other_field = models.IntegerField()
    first_model = models.ForeignKey(MyModel, db_column='mymodel')
    ...

    class Meta:
        # basic django meta Stuff
        verbose_name = 'my other model'

    # the only customisation that make this model special
    class APIMeta:
        pass

Targeted API requirements

To allow this database adapter to work like a relational one, the targeted API must respect some requirements :

  • dynamic-rest installed and all serializers/views must respectively inherit from Dynamic* (DynamicModelSerializer, etc…)

Each API serializer must :

  • Provide the id field

  • Provide the related field (ManyToMany and ForeignKey on Models) as DynamicRelationField

  • Provide the reverse related field. We must, for each ForeignKey and ManyToMany, add a field on the related model’s serializer.

class MenuSerializer(DynamicModelSerializer):
    pizzas = DynamicRelationField('PizzaSerializer', many=True)     # Menu.pizza = ManyToMany

    class Meta:
        model = Menu
        name = 'menu'
        fields = ('id', 'code', 'name', 'pizzas')
        deferred_fields = ('pizza_set', )


class PizzaSerializer(DynamicModelSerializer):

    toppings = DynamicRelationField(ToppingSerializer, many=True)
    menu = DynamicRelationField(MenuSerializer)                     # Add this because Menu.pizza = ManyToMany

    class Meta:
        model = Pizza
        name = 'pizza'
        fields = ('id', 'name', 'price', 'from_date', 'to_date', 'toppings', 'menu')

django-rest-models provide a way to check the consistency of the api with the local models via the django check framework. At each startup, it will query the api with OPTIONS to check if the local models match the remote serializers.

Caveats

Since this is not a real relational database, all feature cannot be implemented. Some limitations are inherited by dynamic-rest filtering system too.

  • Aggregations : is not implemented on the api endpoint, maybe in future releases

  • Complex filtering using OR : all filter passed to dynamic-rest is ANDed together, so no OR is possible

  • Negated AND in filtering: a negated AND give a OR, so previous limitation apply

  • Negated OR in filtering: since the compitation of nested filter is complexe and error prone, we disable all OR. in fact, only some nested of AND is accepted. only the final value of the Q() object can be negated

    for short, you CANNOT :

    Pizza.objects.aggregate()
    Pizza.objects.annotate()
    Pizza.objects.filter(Q(..) | Q(..))
    Pizza.objects.exclude(Q(..) & Q(..))
    Pizza.objects.exclude(Q(..) | Q(..))

but you can :
Pizza.objects.create
Pizza.objects.bulk_create
Pizza.objects.update
Pizza.objects.bulk_update
Pizza.objects.select_related
Pizza.objects.prefetch_related
Pizza.objects.values
Pizza.objects.values_list
Pizza.objects.delete
Pizza.objects.count()
Pizza.objects.filter(..., ..., ...)
Pizza.objects.filter(...).filter(...).exclude(...)
Pizza.objects.exclude(..., ...).exclude(...)
Pizza.objects.filter(Q(..) & Q(..))
Pizza.objects.none()
pizza.toppings.add(...)
pizza.toppings.remove(...)
pizza.toppings.set(...)
pizza.toppings.clear(...)

Specific behaviour

Some specific behaviour has been implemented to use the extra feature of a Rest API :

  • When inserting, the resulting model is returned by the API. the inserted model is updated with the resulting values. This imply 2 things:

    • If you provided default values for fields in the api, these data will be populated into your created instance if it was ommited.

    • If the serializer have some computed data, its data will always be used as a replacement of the one you gave to your models. (cf example: Pizza.cost which is the sum of the cost of the toppling. after each save, its value will be updated)

Support

This database api support :

  • select_related

  • order_by

  • only

  • defer

  • filter

  • exclude

  • delete

  • update

  • create

  • bulk create (with retrive of pk)

  • ManyToManyField

  • ForeignKey*

OAuthToken auth backend

grant_type is provided to the API get_token view by a GET parameter.

Recent framework updates like Django OAuth Toolkit enforce that no GET parameters are used.

Use ENFORCE_POST setting in OPTIONS of api’s DATABASE :

DATABASES = {
    'default': {
        ...
    },
    'api': {
        ...
        'OPTIONS': {
            'OAUTH_URL': '/oauth2/token/',
            'ENFORCE_POST': True,
        }
    },
}

Documentation

The full documentation is at http://django-rest-models.readthedocs.org/en/latest/.

Requirements

  • Python 3.9, 3.10, 3.11, 3.12

  • Django >= 4.2

Contributions and pull requests are welcome.

Bugs and requests

If you found a bug or if you have a request for additional feature, please use the issue tracker on GitHub.

https://github.com/Yupeek/django-rest-models/issues

known limitations

JSONField from postgresql and mysql is supported by django-rest-models, but not by the current dynamic-rest (1.8.1) so you can do MyModel.objects.filter(myjson__mydata__contains=’aaa’) but it will work if drest support it

same for DateField’s year,month,day lookup.

License

You can use this under GPLv3.

Author

Original author: Darius BERNARD. Contributor: PaulWay.

Thanks

Thanks to django for this amazing framework.

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_rest_models-3.0.3.tar.gz (43.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rest_models-3.0.3-py3-none-any.whl (49.3 kB view details)

Uploaded Python 3

File details

Details for the file django_rest_models-3.0.3.tar.gz.

File metadata

  • Download URL: django_rest_models-3.0.3.tar.gz
  • Upload date:
  • Size: 43.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_rest_models-3.0.3.tar.gz
Algorithm Hash digest
SHA256 2200e4649e0919b027d289e635c596598f823388f595360c982c18817e350607
MD5 08aef05273f9150c5f1a74587cdf3ec6
BLAKE2b-256 48c11eb6aaf244ca251f58edc0edd32f81bc123fcfc09c7628bfaa01ea0a0a0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rest_models-3.0.3.tar.gz:

Publisher: publish.yml on Yupeek/django-rest-models

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_rest_models-3.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_models-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d436bd7c6016b39f026b7a4e97659b6086fb8b1c149599db5064599a05f53259
MD5 46752bcc0393de1675f8d730d2e9d6e4
BLAKE2b-256 89f32195f4948c79f5b9e40da4ff401e57b0af02621b5245c258e1f977d48920

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rest_models-3.0.3-py3-none-any.whl:

Publisher: publish.yml on Yupeek/django-rest-models

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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