Django uses / fetches data from other databases

My question below was answered a few years ago in 2013 here . But I am looking for a more reliable and latest solution, if there is one.

I have a Django application app1

and I want to bind data in Django from other databases that are constantly updated on a daily basis. For example: The app1

I have a model model

, where the user must enter a sales order from the juice and the corresponding number of opportunities salesforce. I have tools that dump data from sap to sapdb

and salesforce to salesforcedb

.

My model app1

looks like this:

class SalesOrderMapping(models.Model):
    sales_order = models.CharField("Sales Order #", max_length=10, primary_key=True)
    opportunity_number = models.CharField("Opportunity Number", max_length=30)

    class Meta:
        verbose_name_plural = "Sales Order / Opportunity Mapping"
        ordering = ('sales_order', 'opportunity_number')

    def __str__(self):
        return self.sales_order

      

So as you can see that in the above model I have a sequence_parameter, I want the user to type sales_order

and instead of typing in opportunity_number

it can be an autocomplete textbox (ideally) or a dropdown filled with a table in salesforcedb

. Until now, I could describe the databases in settings.py. Can anyone point me in the right direction?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoportal',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    },

    'sap': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'sapdb',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    },

    'sfdc': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'salesforcedb',
        'USER': 'myuser',
        'PASSWORD': 'mypass',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

      

+3


source to share


3 answers


First you will need a database router for your applications, I will try to do something similar to what you need with the information provided

app1 / routers.py

class App1Router(object):
    def db_for_read(self, model, **hints):

        if model._meta.app_label == 'app1':
            return 'default'
        return None
    def db_for_write(self, model, **hints):
        """
        Attempts to write auth models go to app1.
        """
        if model._meta.app_label == 'app1':
            return 'default'
        return None
    def allow_relation(self, obj1, obj2, **hints):
         db_list = ('default', 'sap', 'sfdc')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None
    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True

      

and in your settings.py file add the following

DATABASE_ROUTERS = ['app1.routers.App1Router',]

      



You can use the inspectdb command to create models of existing databases, for example:

./manage.py inspectdb --database "sap"

      

I hope this helps

Source: personal experience and Django documentation https://docs.djangoproject.com/en/1.10/topics/db/multi-db/

+3


source


Django supports multiple databases, as described here:

https://docs.djangoproject.com/en/1.10/topics/db/multi-db/



Assuming you set your primary database as your primary database, you will need to specify which database you want to access when you read from another database.

+1


source


Other answers have already mentioned the nature of DB plurality and related to docs.

You might want to use something like the Django REST Frame to trigger access to other tables as a mini-service for the rest of the application, instead of trying to switch context DB calls in your Views code.

eg. if you need table lookup data SAP

or SalesForce

, you are making an Ajax request to your backend with this id. It will send you a JSON response with what you need.

This way you can abstract the relationship and change implementation details in the future without rewriting a lot of code.

0


source







All Articles