I am trying to use multiple different DB in a single app(todo). I am using Djongo package for dealing mongodb.
Settings.py
DATABASES = {
'default':{},
'sql_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'my_db',
'USER': '******',
'PASSWORD': '***',
'HOST': 'localhost',
'PORT': '5432',
},
'mongodb':{
'ENGINE': 'djongo',
'NAME': 'mongo_db'
}
}
todo/models.py
class Task(models.Model):
todo = models.CharField(max_length=200)
status = models.BooleanField(default=False)
def __str__(self):
return self.todo
todo/serializers.py
class TodoSerializer(serializers.ModelSerializer):
class Meta:
model = Task
fields = '__all__'
todo/views.py
@api_view(['POST'])
def todoCreate(request):
serializer = TodoSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
serializer.save(using='mongodb')
return Response(serializer.data)
it successfully saved the record into 'sql_db' but not save in the 'mongodb'.