Django + mongodb

I am trying to use MongoDB with Django. I followed this guide to set it up so that all the necessary things are installed. MongoDB + Django Tutorial My problem is this: When I try to run cities = City.objects.get()in mine, views.pyI 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 is as follows

    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

{
     "city" : "ACMAR",
     "loc" : [
        -86.51557,
        33.584132
     ],
     "population" : 6055,
     "state" : "AL",
     "_id" : "35004"
}
+5
source share
2 answers

I have found a solution. The problem was that I did not know how to choose which collection to use. So, Django created a new collection called "myAppName_cities".

django, , -, .

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"
+6

,

cities = City.objects.all()

City.objects.get . , .

0

All Articles