Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django + MongoDB

Im trying to use MongoDB together with Django. I've followed this guide to set it up so all necessary things is installed. MongoDB + Django tutorial My problem is as follows: When trying to run cities = City.objects.get() in my views.py I get the following error:

DoesNotExist at /GetAllCities/
        City matching query does not exist.

My MongoDB looks like this

Databasename = "exjobb"
Collectioname = "cities"`

And it contains 30,000 rows of data, it works with my Rails and PHP application.

My model class looks like this

    from django.db import models
    from django.core.urlresolvers import reverse
    from djangotoolbox.fields import ListField, EmbeddedModelField

    # Create your models here.
    class City(models.Model):
        city = models.TextField()
        loc = models.TextField()
        population = models.IntegerField()
        state = models.TextField()
        _id = models.IntegerField()

        def __unicode__(self):
            return self.city

And one row in the database looks like this

{
     "city" : "ACMAR",
     "loc" : [
        -86.51557,
        33.584132
     ],
     "population" : 6055,
     "state" : "AL",
     "_id" : "35004"
}
like image 243
JOSEFtw Avatar asked Apr 22 '13 19:04

JOSEFtw


1 Answers

I found a solution. The problem was that I didn't know how to choose which collection to use. So Django created a new collection named "myAppName_cities".

To tell django which collection to use, just add a meta class like this.

class City(models.Model):
    city = models.TextField()
    loc = models.TextField()
    population = models.IntegerField()
    state = models.TextField()
        #Specify collection in the MongoMetaclass
    class MongoMeta:
        db_table = "cities"
like image 102
JOSEFtw Avatar answered Sep 28 '22 03:09

JOSEFtw