Django-Dynamic-model
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
pip install django-filter
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, first let’s import the RemoteModel
from django_remote_model.remote_model import RemoteModel
remote_model = RemoteModel(model_name, provider_url,view_set_url, Remote_Model_Api_Key, KEY_Value, has_permission=false)
Explanation for the arguments: Argument | Function — | — model_name | The name of the model provider_url | The url for the ProviderViewGenerator view_set_url | The url for the ModelViewSetGenerator Remote_Model_Api_Key | The api Key KEY_Value | The Key has_permission | default False, Whether the model has permissions or not (useful when using the admin)
So far the following methods are tested
remote_model.model.objects.all()
remote_model.model.objects.get()
remote_model.model.objects.filter()
remote_model.model.objects.order_by()
remote_model.model.objects.distinct()
remote_model.model.objects.count()
remote_model.model.save()
remote_model.model.delete()
Best Practices
It’s better to have only one remote_model instance per api and to import it wherever you want it
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()
Permission
By default the remote model does not have permission of its own because it’s not generated from a migration, but you can create a migration for it by setting has_permission to True
but bear in mind that you will need to delete those permission by hand if you decided to change or remote the remote model
Usage with the Django Admin
The model can be used normally with the Django admin and it supports most of the admin functionality
from django.contrib import admin
from .models import remote_model
admin.site.register(remote_model.model)
Metadata
Release files for django-remote-model 0.1.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_remote_model-0.1.7.tar.gz | 8.1 kB | Without provenance | Details for django_remote_model-0.1.7.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_remote_model-0.1.7-py3-none-any.whl | Python 3 | none | any | Details for django_remote_model-0.1.7-py3-none-any.whl |
Total release size: 16.0 kB
Release files / django_remote_model-0.1.7.tar.gz
| Download URL | django_remote_model-0.1.7.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe5269f87849f69029b7ef0ff1249becddf6ca12d1e2a8cd56b6e018939322cc
|
|
BLAKE2b-256 checksum How to use checksums |
3c996370c48cfc468e74453ec8cdc4f6f4daa44b28ab34c4509a9f71527cb9e7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4
|
Release files / django_remote_model-0.1.7-py3-none-any.whl
| Download URL | django_remote_model-0.1.7-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7c6fbee0ecfd7154a2555eda3987c1616e436b6762fa712405095c50759cf3e4
|
|
BLAKE2b-256 checksum How to use checksums |
54986af0288b04548c477be8ff0e99aa0fc16aaa4f95b552c339943e151d1469
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4
|
Security for django-remote-model 0.1.7
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.