# Modify the model-table from another machine/outside Django

**URL:** <https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921>\
**Category:** Using Django\
**Created:** [March 6, 2021, 8:19pm UTC](https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921 "2021-03-06T20:19:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakobhenningjensen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jakobhenningjensen/32/3751_2.png) [@Jakobhenningjensen](https://forum.djangoproject.com/u/Jakobhenningjensen)\
**Post date:** [March 6, 2021, 8:19pm UTC](https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921/1 "2021-03-06T20:19:35Z")

</div>

Say I have a model with 3 fields `field1, field2, field3`. I want to alter field2 based on the outcome on a heavy function, thus that function is computed elsewhere (on another machine, outside the Django app) with some more processing power. How do I, in a secure way, push the changes that is computed at Machine A back to the model-table of Django (which runs on Machine B)?

It is doable with pandas like:

```python
Import pandas as pd

df= pd.from_sql(myapp_model)
df = my_heavy_function()
df.to_sql(myapp_model)

```

but I doubt it’s the best way to do it.

Furthermore I’ve thought just cloning the Django project to Machine A (the computation one) and have one fuction which is called that does the update using e.g

```python
from myapp.models import my_model
df = my_heavy_function()
user= my_model.get(user=my_user)
user.field2=df["field2"]
user.save()

```

but that sounds a bit overkill to me

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 7, 2021, 12:43am UTC](https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921/2 "2021-03-07T00:43:54Z")

</div>

You’ve got a couple different options -

- Have your external program update the database directly, bypassing Django completely.  
or
- Create a JSON endpoint where your external program submits the value to be updated. Your program becomes a “client” just like any other - using _some_ authentication method for Django to check.

(Other ideas I thought of basically boil down to variations of these two.)

---

<div class="post-metadata">

**Author:** ![Jakobhenningjensen](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jakobhenningjensen/32/3751_2.png) [@Jakobhenningjensen](https://forum.djangoproject.com/u/Jakobhenningjensen)\
**Post date:** [March 7, 2021, 5:46am UTC](https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921/3 "2021-03-07T05:46:47Z")

</div>

I think your first suggestion is just like my “pandas-workaround”, right? That is definitely the easiest way I think, but I wonder if it’s secure e.g locking if Django and the external program tries to update the database at the same time.

Would you have any suggestions (or can you point me at a direction/documentation) of how to implement your second suggestion?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 7, 2021, 1:23pm UTC](https://forum.djangoproject.com/t/modify-the-model-table-from-another-machine-outside-django/6921/4 "2021-03-07T13:23:13Z")

</div>

> [@Jakobhenningjensen](#):
>
> I think your first suggestion is just like my “pandas-workaround”, right?

It’s similar in principle, but without the “overkill”. Instead of re-implementing your Django installation, you would just be using the [Python db api](https://www.python.org/dev/peps/pep-0249/) to access the database directly from your program and issuing SQL statements to update the tables.

> [@Jakobhenningjensen](#):
>
> e.g locking if Django and the external program tries to update the database at the same time

This is _precisely_ what database engines are designed to _do_. Databases like PostgreSQL, MySQL/MariaDB, Oracle, MS SQL, DB2, etc, exist to allow for multiple processes to update data and to keep everything “in order”. (Now, if you’re using Sqlite, that’s a different issue.) Keep in mind that when you’re running Django in a production-ish environment, you’re already running multiple instances of your project as separate processes, and so you’re already doing something like this.

> [@Jakobhenningjensen](#):
>
> Would you have any suggestions…

For a single endpoint with limited requirements, I _wouldn’t_ recommend implementing DRF. That would also seem to be “overkill” to me. I’d do it “manually”, I don’t think it would be all-that-much work.

On the Django side, you could create a view that accepts a JSON object (accessed in the view using request.body) and updates the appropriate model. That JSON object can be defined to contain whatever data necessary for the view to process the submission. You would then define a URL that would map a request to that view.

The sending side can then issue a POST to that url using the [Requests package](https://requests.readthedocs.io/en/master/) to submit the updates.

If you need to add a layer (or two) of security, you could include some sort of credentials (perhaps a userid & password, or maybe just some type of token) in the JSON data being submitted, then validate those credentials within the view. You could also use the [get\_host](https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.get_host) method to identify where the request is coming from. (Note, I wouldn’t rely on this alone, because IP addresses can be spoofed.)
