0
votes

I'm developing some project in Django, something to manage assets in warehouses. I want to use two databases to this. First is sqlite database, which contains any data about users. Second is mongoDB database,in which want to store all the data related to assets. The question is, how to tell to my model classes, which database they should use (models responsible for user registration etc - sqlite, models responsible for managing assets data - mongoDB)? I read about DATABASE_ROUTERS and using Meta classes, but it's solutions for supported databases by Django (or maybe I don't know something), I dont know if it's good and possible to integrate it with mongoengine.

Thanks for any tip!

2

2 Answers

0
votes

This may help you:

I fully agree with to use database router. What I did is, I have used single admin interface to handle multiple databases. Note that authentication for all apps are stored in the default database.

Settings.py

# Define the database manager to setup the various projects
DATABASE_ROUTERS = ['manager.router.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {'app1': 'db1', 
                         'app2':'db2'}

DATABASES = {
    #For login authentication of both app I have used postgres sql
    'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2', 
            'NAME': 'fail_over',                    
            'USER': 'SomeUser',                      
            'PASSWORD': 'SomePassword',                  
            'HOST': '127.0.0.1',                     
            'PORT': '',                      
    },
    # Set this parameters according to your database configuration
    'db1': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_DIR, 'yourdatabasename.db'),
    },
     # Set this parameters according to your database configuration
    'db2' : {
      'ENGINE' : 'django_mongodb_engine',
      'NAME' : 'my_database'
   }
}

Sample Models

# Create your models here for app1.
class Modelapp1(models.Model):
    field1 = models.TextField(max_length=100)
    field2 = models.TextField(max_length=200)

    class Meta:
        app_label = 'app1'

    def __unicode__(self):
        return self.field1
# Create your models here for app2.

class Modelapp2(models.Model):
    field1 = models.CharField(max_length=25)

    class Meta:
        app_label = 'app2'

    def __unicode__(self):
        return self.field
0
votes

I found a solution, It's very simple. If you want your model to use mongoDB database, just create model class witch Document parameter (or EmbeddedDocument, for example class Magazine(Document):). but if you prefer default database type defined, just create class, like in django documentation (example class Person(models.Model):).