Question

I'm using django+mongoengine.

Im following the official tutorial but I have no idea how to introduce data in my database.

I don't know if I had to use django console, python console or mongodb

Here is the ling with the tutorial: MongoengineTutorial

Using mongo I find the following error

(reb_env)root@marcproves:/REBORN/REBORN_PROJ# mongo
MongoDB shell version: 2.4.9
connecting to: test
Server has startup warnings: 
Tue Apr  1 05:37:35.300 [initandlisten] 
Tue Apr  1 05:37:35.300 [initandlisten] ** WARNING: You are running in OpenVZ. This is known to be broken!!!
Tue Apr  1 05:37:35.301 [initandlisten] 
> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Tue Apr  1 06:22:38.046 ReferenceError: User is not defined
> show dbs
local   0.03125GB
reborn  0.203125GB
> use reborn
switched to db reborn
> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Tue Apr  1 06:22:51.791 ReferenceError: User is not defined

I just created an app and added the following code from the tutorial:

  GNU nano 2.2.6                             File: models.py                                                                  

//from django.db import models
from mongoengine import *
# Create your models here.
class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)

class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User, reverse_delete_rule=CASCADE)
    tags = ListField(StringField(max_length=30))
    comments = ListField(EmbeddedDocumentField(Comment))
    meta = {'allow_inheritance': True}

class TextPost(Post):
    content = StringField()

class ImagePost(Post):
    image_path = StringField()

class LinkPost(Post):
    link_url = StringField()

class Comment(EmbeddedDocument):
    content = StringField()
    name = StringField(max_length=120)

UPDATED: Runned in django shell

(reb_env)root@marcproves:/REBORN/REBORN_PROJ# python manage.py shell
Python 2.7.3 (default, Sep 26 2013, 20:03:06) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mongoengine import *
>>> connect('reborn')
MongoClient('localhost', 27017)
>>> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
NameError: name 'User' is not defined
>>>  

And the same error, User is not defined, what should I do ?

Was it helpful?

Solution

Okay, I finally solve the problem.

In models I had to declare how the db is, as always...

(models.py)

# Create your models here.
from mongoengine import *
class User(Document):
    email = StringField(required=True)
    first_name = StringField(max_length=50)
    last_name = StringField(max_length=50)
class Comment(EmbeddedDocument):
    content = StringField()
    name = StringField(max_length=120)


class Post(Document):
    title = StringField(max_length=120, required=True)
    author = ReferenceField(User)
    tags = ListField(StringField(max_length=30))
    comments = ListField(EmbeddedDocumentField(Comment))
    meta = {'allow_inheritance': True}

class TextPost(Post):
    content = StringField()

class ImagePost(Post):
    image_path = StringField()

class LinkPost(Post):
    link_url = StringField()

In views.py(or python console either)....

from django.shortcuts import render 
# Create your views here.
from django.http import HttpResponse  
from game.models import *  //Import the models 
from mongoengine import * //Import things like connect

def testview(request):  
    connect('reborn')//Connect to DB
    //Let's add some data!
    article = User(email = 'test@title.com',first_name = 'test content')
    article.save()
    return HttpResponse("SAVED")

OTHER TIPS

Those examples are Python code. You run them in the Django shell, or in your app's code.

Try type import * from app.models in shell before adding data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top