Search code examples
pythondjangomongodbdjango-modelsweb-deployment

Retrieving data from Mongodb and show it on front-end using django


I have a mongodb database named as world, which has two collection from before city, languages. I want to show the data of my collection on the web, how can i do it.

currently i know to create collection in models.py and migrate it. like; first we have to edit databases[] in setting.py

DATABASES = {
       'default': {
           'ENGINE': 'djongo',
           'NAME': 'world',
       }
   }

in models.py i creted a class and migrated it using python manage.py migrate

class Destination(models.Model):
    name= models.CharField(max_length=100)
    img=models.ImageField(upload_to='pics')
    desc=models.TextField()

and i'm able to retrieve data from Destination by using below code in views.py

from django.shortcuts import render
from .models import Destination
def index(request):
    dests=Destination.objects.all()
    return render(request,'index.html',{'dests':dests})

My question is my collection/class is already available in the database ( city, language) and i'm not creating it contrary to Destination which was defined by me. then how to show data of city collection of world database on the front-end. kindly looking for help.


Solution

  • If I got you properly, then you have a MongoDB database called world. There you stored city and languages before you started to set up Django. Then you added a Destination model, thus created a new collection. For now, you're looking for a way how to get city and languages collections data similar way as you do with Destination.

    So there are multiple ways how you could handle it:

    1. Create Django models for city and languages collections (define fields that you have in existing collections):
    class City(models.Model):
        field1 = ...
        field2 = ...
    
        class Meta:
            db_table = 'city'  # important, should be your existing collection name
    
    class Language(models.Model):
        field3 = ...
        field4 = ...
    
        class Meta:
            db_table = 'languages'  # important, should be your existing collection name
    

    Now you're ready to use City and Language the same way as you do with the Destination model.

    1. Use PyMongo (this is already installed as you're using Djongo). So your snipped will look something like:
    from django.shortcuts import render
    from .models import Destination
    import pymongo
    
    # default localhost connection URL
    MONGO_URL = 'mongodb://localhost:27017'
    connection = pymongo.MongoClient(MONGO_URL)
    mongo_db = connection.world
    
    
    def index(request):
        collection_city = db['city']
        collection_languages = db['languages']
    
        cities = list(collection_city.find())
        languages = list(collection_languages.find())
        dests=Destination.objects.all()
        return render(
            request,
            'index.html', 
            {
                'dests': dests, 
                'cities': cities,
                'languages': languages
            }
        )
    

    I'd use option 1, as it allows you to keep the project coherent.