Django + mongoengine, connect to mongo when used as auxiliary database - django

I'm trying to connect to mongodb using mongoengine.
Mysql is my default database, and I have 'mongoengine.django.mongo_auth' in my installed apps. I removed the 'AUTH_USER_MODEL = 'mongo_auth.MongoUser'' due to errors about not having a default connection.
I use mongo with celery so I don't think there's a problem with the setup. This is how I am attempting to connect - the code is in views.py
from mongoengine import connect
my_connect = connect('my_db', alias='mongo')
test = test(name='a_name', desc='a desc')
test.save(using='mongo')
my_connect.connection.disconnect()

Have finally managed to sort this out:
#settings.py
from mongoengine import register_connection
register_connection(alias='default',name='db_name')
#models.py
from mongoengine import Document, StringField (etc)
class my_col(Document):
field_a = StringField()
#in your app
from mongoengine import connect
my_con = connect('db_name', alias='default')
item = my_col(field_a='something')
item.save()
my_con.disconnect()

Related

Database error in django+mongodb objects.fiter with boolean field

I am using djongo library to handle mongodb, but still getting database error while filtering queryset using a boolean field.
Error: No exception message supplied,
django.db.utils.DatabaseError
from django.contrib.auth import get_user_model
User = get_user_model()
users = User.objects.filter(isVerified=True)
first of all django is a backend framework not mongodb libraray
secound of all you are using django orm which doesn't support noSQL databases
right now django orm support only relational dbms
for handle mongodb PyMongo is a good library

Graphene query hangs indefinitely when testing with pytest

I am trying to test my backend, written in Django 2.2.2 and Python 3. I created some graphql queries which are definitely working when testing with the graphql web interface. When testing with pytest and the graphene test client, however, these queries would always hang indefinitely. I put together a reproducible example which is actually based on the code example from the graphene-django documentation.
test_example.py:
import pytest
import graphene
from graphene_django import DjangoObjectType
from graphene.test import Client
from django.db import models
class UserModel(models.Model):
name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
class User(DjangoObjectType):
class Meta:
model = UserModel
class Query(graphene.ObjectType):
users = graphene.List(User)
def resolve_users(self, info):
return UserModel.objects.all()
schema = graphene.Schema(query=Query)
client = Client(schema)
def test_user():
query = '''
query {
users {
name,
lastName
}
}
'''
result = client.execute(query)
assert 0 # dummy assert
This example behaves in the same way (stalls forever, no errors). I am using the latest graphene-django (2.3.2) and pytest (4.6.3). I should probably also mention that I'm running this inside a Docker container. Any ideas why this happens? Is this a bug in the graphene-django library?
I found the answer myself after a while digging through the documentation. Pytest needs permission to use the database. So the issue is solved by simply adding the pytest mark #pytest.mark.django_db before the test. As an alternative the whole module can be marked to allow database access by using pytestmark = pytest.mark.django_db. See pytest-django docs.
The documentation says the tests will fail if db access is not granted, so I would not expect them to stall forever.

Neomodel / py2neo - 'NodeMeta' object is not iterable

I am new to Neomodel and Neo4J. I am running a remote Neo4J server on Amazon Ec2, developing locally and running into issues when trying to access the project via the browser.
When attempting to access the project via my browser, I get the following error:
TypeError at /
'NodeMeta' object is not iterable
Here is my admin.py file:
from django.contrib import admin
from app.admin import BaseAppAdmin
from .nodes import TwitterPost
class TweetAdmin(BaseAppAdmin, admin.ModelAdmin):
list_display = ('postId', 'text')
search_fields = ('text',)
admin.site.register(TwitterPost, TweetAdmin)
I have been reading through the docs in py2neo on batch read/write functionality, but I am not sure how to implement this. ( http://book.py2neo.org/en/latest/batches/#py2neo.neo4j.ReadBatch )
Any help or guidance is greatly appreciated!
I am presuming TwitterPost is a StructuredNode definition? It looks like your trying to register it with django admin however neomodel nodes don't integrate with django admin (yet) but patches are most welcome :-)
Rob

Unable to Sync Tables in Django

Currently programming in Python Django 1.4. In my files I have written CREATE TABLE functions for productos and clientes to be created in the MySQL database. When I checked with python manage.py sqlall ventas (ventas being the parent directory of productos and clientes), it outputs that snippet of code. However, when I tried to access them under localhost admin, I got the
1146, "Table 'demo.ventas_cliente' doesn't exist" error. And these 2 tables do now show up in MySQL.
Initially I had dropped these 2 tables because there were some DB errors. I ran syncdb again but does not seem to retrieve those 2 tables. What seems to be wrong?
I strongly suggest you don't type MySql commands directly, this may throw you many errors in the future. To create tables in Django you must create models. For example:
1) In ./Yourproject/apps/yourDBName create __ init __.py
2) in settings.py under "installed apps" add your new app. Example: yourproject.apps.yourapp
4) execute "python manage.py runserver"
5) create models.py into your new app
6) now you can create your models like that:
class myTable(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField()
7) if you want to access your new table from the admin, create admin.py in your app and type:
from django.contrib import admin
from models import myTable
admin.site.register(myTable)
#here, you can add more tables to the admin in the future
also, if you need to use foreign keys:
class parentTable(models.Model):
idParent = models.AutoField(primary_key=True)
parentName = models.CharField(null=False)
class childTable(models.Model):
idChild = models.AutoField(primary_key=True)
MyParentName = models.ForeignKey(parentTable, to_field='parentName')
childName = models.CharField(null=False)
MORE INFO: https://docs.djangoproject.com/en/dev/topics/db/models/

Differences between Django on AppEngine and Django on my Linux Server?

I am a Django beginner, and I want to make this tutorial as exercise: http://www.joeyb.org/blog/2009/05/28/django-based-blog-on-google-app-engine-tutorial-part-1
The thing is that this tutorial is for AppEngine, but I want to do the tutorial in my Linux Development machine using a common database.
I have noticed that there are few differences:
In the Models:
from appengine_django.models import BaseModel
from google.appengine.ext import db
class BlogPost(BaseModel):
title = db.StringProperty()
uri = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
teaser = db.TextProperty()
teaser_html = db.TextProperty()
content = db.TextProperty()
content_html = db.TextProperty()
tags = db.StringProperty()
These imports are different:
from appengine_django.models import BaseModel
from google.appengine.ext import db
If I change this by:
from django.db import models
It will work?
Then I noticed one more reference to AppEngine:
from google.appengine.api import users
from google.appengine.ext.db import djangoforms
What imports should I use here to make this compatible with my Django on my Linux development server?
Best Regards,
AppEngine is not Django. There are ways of getting Django to work (more or less) on AppEngine, but that tutorial is specifically for AppEngine, not Django.
If you want to learn Django, do a Django tutorial. There are enough out there on the web.