0

When testing my connection to MongoDB I put the following in my models.py. I had to change some parts of it (JSONField) because I'm using newer version of Django 1.8.7 which seems to have made it not work:

from django.db import models
from jsonfield import JSONField

class Post(models.Model):
    title = models.CharField(max_length=200)
    text = models.TextField()
    tags = JSONField()
    comments = JSONField()

Then I run:

from testapp.models import Post
post = Post.objects.create(
    title='Hello MongoDB!',
    text='Just wanted to drop a note from Django. Cya!',
    tags=['mongodb', 'django']
)
post.comments = []
post.comments.extend(['Great post!', 'Please, do more of these!'])
post.save()

The last line of the script results in an error:

TypeError: int() argument must be a string or a number, not 'ObjectId'

My config: Django 1.8.7, Python 2.7, Django-nonrel 1.6, django-mongodb-engine 0.6.0

Peter G.
  • 7,816
  • 20
  • 80
  • 154
  • 1
    Your description of your stack makes no sense. Django-nonrel *replaces* Django, so you can't be using version 1.6 of that alongside 1.8.7 of Django. – Daniel Roseman Nov 25 '15 at 13:43
  • 1
    And since you're using django-mongodb-engine, there's no reason to use JSONField at all; use [ListField or DictField](https://django-mongodb-engine.readthedocs.org/en/latest/topics/lists-and-dicts.html). – Daniel Roseman Nov 25 '15 at 13:43
  • Right, Django-nonrel should replace Django, but in my case it didn't I'm going to make a fresh install. – Peter G. Nov 25 '15 at 13:53
  • Thanks, both suggestions were needed to make it connect to MongoDB. – Peter G. Nov 25 '15 at 14:11
  • I installed Django because of the error `ImportError: No module named django.core.management`. After reinstallation the error appears again. – Peter G. Nov 25 '15 at 14:33
  • By doing that you've overwritten django-nonrel. You can't have them both; nonrel is a *fork* of Django and replaces it, as I said above. If you're getting an error, you should ask about that (in a separate question), describing exactly what you were doing along with the full traceback. – Daniel Roseman Nov 25 '15 at 14:44

0 Answers0