0

I'm new to Django and I'm making a Django app where I'm supposed to use MySQL and MongoDB through py-mongo. (I'm trying to use only py-mongo, not with mongoengine)

I created an app 'test-app' with a model 'Books' and a CRUD views which seem to work fine (all books are saved and retrieved).

However, for some reason I can't seem to find the Books back neither in the MySQL db nor the MongoDB. I'm not sure which database the model is using.

There are also settings for Redis, but even after restarting the server the model entries are there, so I guess it is not in the cache.

my settings are:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '127.10.0.1',
        'NAME': 'test_db',
        'USER': 'test_user',
        'PASSWORD': 'test_pass',
    }
}

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '127.10.0.1:6379',
        'OPTIONS': {
            'DB': 0,
        },
    },
}

MONGODB = {
    'default': {
        'HOST': '127.10.0.1',
        'PORT': '27017',
        'NAME': 'django',
    }
}

And my requirements.txt:

Django>=1.8,<1.9
docker-compose>1.11
django-redis-cache>1.7
pymongo>=3.4
MySQL-python>=1.2

The migrations file seems to have MongoDB syntax, not SQL syntax, which makes me think it is actually stored in MongoDB:

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Book',
            fields=[
                ('number', models.IntegerField(serialize=False, primary_key=True)),
                ('author', models.CharField(max_length=200)),
            ],
        ),
    ]
Kemeia
  • 826
  • 2
  • 9
  • 24
  • Seems like a badly designed system. Why complicate matters with both an SQL and a noSQL system running side by side? Why not use just one or the other? FYI mysql is perfectly capable of storing non relational data for example with the help of it's JSON data type – e4c5 May 03 '17 at 06:54
  • This is the assignment I got. I guessed the MySQL should store all the info that for the Django installed apps (like auth and so), and the MongoDB for the models. – Kemeia May 03 '17 at 07:11

2 Answers2

0

you need to mention which connection you are going to use for saving the data. go through django documentation for using different connection objects to insert retrieve data from multiple databases.

and as you mentioned DATABASES to be mysql it will be default to whatever you create or delete

Exprator
  • 26,992
  • 6
  • 47
  • 59
0

Django wouldn't randomly decide to use MongoDB for your models; even if it knew how to, you simply haven't included Mongo in your DATABASES setting. The only place Django will look for your models is in the MySQL database.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thanks, that is helpful. Though I don't see the 'book' table in mysql either (maybe cause the project is composed with docker?). And why is the migration file not in SQL syntax? – Kemeia May 03 '17 at 08:42