Django Remote Model
Project description
This package is used to connect two independent django projects together via rest apis while providing the same interface that a normal django model class will have.
Dependencies
The script uses Django REST framework. You can install it by following this tutorial
Installation
pip install django_remote_model
Usage
They say a good example is worth 100 pages of API documentation. so let’s get started with an example
We will have two projects: - The provider - The consumer
we will assume that the provider has a model that the consumer need to access, let’s say this model is called ‘Product’.
so to create the apis to make it available to the consumer to connect to the provider we will use the ProviderViewGenerator and ModelViewSetGenerator methods from the package
# provider urls
from django_remote_model.provider.provider_view_set import ProviderViewGenerator, ModelViewSetGenerator
provider_view = ProviderViewGenerator(<model_class>, 'Remote-Model-Api-Key', 'KEY_Value')
model_view_set = ModelViewSetGenerator(<model_class>,'Remote-Model-Api-Key', 'KEY_Value')
router = SimpleRouter()
router.register('api/model', model_view_set)
urlpatterns =[
path('api/<model>/provider/', provider_view),
] + router.urls
Now that we half the apis ready, it’s the consumer turn to connect to it
from django_remote_model.remote_model import RemoteModel
remote_model = RemoteModel('<model_name>',<provider_url>,<view_set_url>,'Remote-Model-Api-Key', 'KEY_Value')
Those are the tested query methods
remote_model.model.objects.all()
remote_model.model.get()
remote_model.model.filter()
remote_model.model.order_by()
remote_model.model.distinct()
remote_model.model.count()
It’s better to have only one remote_model instance per api and to import it wherever you want ti
The remote_model instance will update the model inside of it if any update happens in the fields from the provider but if you want to force update it you can run remote_model.update()
Usage with the Admin
The model can be registered normally like this
from django.contrib import admin
from .models import remote_model
admin.site.register(remote_model.model)
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
Built Distribution
Hashes for django_remote_model-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b57bf12513deee3ddb496c6082dc94b007c9b9bad19fdbab8bdefd9fec3ef021 |
|
MD5 | df32d7b31f9e7d19d22f5b2120a925da |
|
BLAKE2b-256 | 7b4577d7d58f032543c35c1879855521ad1d8416019acba45f2b1b8566fe89cb |
Hashes for django_remote_model-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0205d0dd3c7307121436c2d38279d6c81464b8b8bc92ee501f5d5ff8bacc0966 |
|
MD5 | 8678260c6a72552b59cb01c6f92decaa |
|
BLAKE2b-256 | da4a5642846ae9c491f771da7620068bdc9dbd8a5618fa9f388c2faf66583925 |