Mongoengine python3 Embedded Document has no attribute _is_document - django

I am creating my a document having a field as EmbeddedDocument using mongoengine.
But I am getting the following error :
AttributeError: 'EmbeddedDocument' object has no attribute '_is_document'
Doing some reasearch I found that the code of mongoengine is written in python 2 and do make it work properly install it using pip3. Did the same still not working.
class DataImportNodeInfo(EmbeddedDocument):
provider_id = fields.IntField(required=False)
carrier_name = fields.StringField(required=False)
basepath = fields.StringField(required=False)
log_tobe_imported = fields.ListField(required=False)
class DataImportConnectionInfo(EmbeddedDocument):
host = fields.StringField(required=True)
user_login = fields.StringField(required=False)
user_pwd = fields.StringField(required=False)
class DataImportNode(DynamicDocument):
# Meta variables
meta = {
'collection': str(KonnectDAConstants.DATA_IMPORT_NODES)
}
cmd = fields.StringField(required=True)
import_source = fields.StringField(required=True)
import_type = fields.StringField(required=True)
active = fields.BooleanField(required=True)
info = fields.EmbeddedDocument(DataImportNodeInfo, required=True)
connection = fields.EmbeddedDocument(DataImportConnectionInfo, required=True)
AttributeError: 'EmbeddedDocument' object has no attribute '_is_document'

You should use EmbeddedDocumentField instead of EmbeddedDocument in DataImportNode:
info = fields.EmbeddedDocumentField(DataImportNodeInfo, required=True)
connection = fields.EmbeddedDocumentField(DataImportConnectionInfo, required=True)
Since the EmbeddedDocumentField is used to reference the EmbeddedDocument.

Related

Getting - Can't install traversal 'has' exists on NodeSet, error when matching Node

I'm using Django with Neomodel and django_neomodel to connect to AuraDB and fetch data.
In my views I'm doing:
def homePage(request):
name = request.user.last_name + ', ' + request.user.first_name
my_person_node = Person.nodes.get(person_name=name)
...
My Person model:
class Person(DjangoNode):
id = UniqueIdProperty(primary_key=True)
person_id = StringProperty(unique_index=True)
person_name = StringProperty(unique_index=False)
service_position = StringProperty(unique_index=False)
has = Relationship('Research_Topic', 'has', model=Person_has_Research_Topic)
is_teaching = RelationshipTo('Course', 'is_teaching')
class Meta:
app_label = 'profiles'
def __str__(self):
return self.person_name
and 'has' relation model:
class Person_has_Research_Topic(StructuredRel):
type = StringProperty()
This throws an error: ValueError: Can't install traversal 'has' exists on NodeSet
Maybe you should change the class attribute 'has'. I met this problem when using 'source' as attribute name and solved it by changing a new name.

How to update Boolean Value in Flask SQLAlchemy

I am currently using this code
update = user.query.filter_by(uid=id).update(dict(approved=True))
usr.session.commit()
But it gives this as error:
UPDATE
USER MODEL
usr = SQLAlchemy(app)
Class user(usr.Model):
index = usr.Column(usr.Integer(), primary_key=True)
username = usr.Column(usr.String())
uid = usr.Column(usr.String(8))
approved = usr.Column(usr.Boolean(), default=None)
instead of this
update = user.query.filter_by(uid=id).update(dict(approved=True))
Do this
update = user.query.filter_by(uid=id).first()
update.approved = True
usr.session.commit()
This will Solve your Problem

AttributeError: _auto_id_field Django with MongoDB and MongoEngine

I am using mongoengine with Django
Below is my Model class
class MyLocation(EmbeddedDocument):
my_id = IntField(required=True)
lat = GeoPointField(required=False)
updated_date_time = DateTimeField(default=datetime.datetime.utcnow)
My Views.py
def store_my_location():
loc = MyLocation(1, [30.8993487, -74.0145665])
loc.save()
When I am calling the above method I getting error AttributeError: _auto_id_field
Please suggest a solution
I suggest using the names when you save the location. Since class definition does not include how you put in these keys that is why we need to use the name to define them.
def store_my_location():
loc = MyLocation(my_id=1, lat=[30.8993487, -74.0145665])
loc.save()
This should work.
One more appraoch is to write everything in MyLocation class.
class MyLocation(EmbeddedDocument):
my_id = IntField(required=True)
lat = GeoPointField(required=False)
updated_date_time = DateTimeField(default=datetime.datetime.utcnow)
def create(my_id,lat):
location=MyLocation(my_id=my_id,lat=lat)
location.save()
return location
def store_my_location():
loc = MyLocation.create(1,[30.8993487, -74.0145665])

AttributeError: '_Option' object has no attribute '_sa_instance_state' in flask

I am using Flask for my app and getting AttributeError: '_Option' object has no attribute '_sa_instance_state' error. have no idea what it is about. I google it and it seems that it is a SQL-Alchemy issue. Could you please help me with that?
here is my models.py:
#current table is used to make navigation hierarchy. eg. menu/submenu
#eg. About Us (History, Mission, Vision)
menu_hierarchy = db.Table('menu_hierarchy',
db.Column('parent_id', db.Integer, db.ForeignKey('menu.id')),
db.Column('child_id', db.Integer, db.ForeignKey('menu.id'))
)
class Menu(db.Model):
"""Menu is used for websites navigation titles.
eg. Home/About Us/Blog/Contacts/and etc"""
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(255))
title_eng = db.Column(db.String(255))
alias = db.Column(db.String(255))
menu_type = db.Column(db.String(10))
ordering = db.Column(db.SmallInteger, default = '1')
check_out_time = db.Column(db.DateTime)
access = db.Column(db.String(30))
published = db.Column(db.SmallInteger, default = '1')
parent_id = db.relationship('Menu',
secondary = menu_hierarchy,
primaryjoin = (menu_hierarchy.c.parent_id == id),
secondaryjoin = (menu_hierarchy.c.child_id == id),
backref = db.backref('menu_hierarchy', lazy = 'dynamic'),
lazy = 'dynamic')
content = db.Column(db.String)
content_eng = db.Column(db.String)
image = db.Column(db.String(350))
It appears the problem is related to an integer (ID) being interpreted as an ORM object.
See this SO post which includes a relevant code sample.

Django: TypeError: 'branch__organisation' is an invalid keyword argument for this function

I have 3 models:
class Organisation(CommonInfo):
name = models.CharField(max_length=50)
gems = models.PositiveIntegerField(blank=True,default=0,null=True)
class Branch(models.Model):
email = models.EmailField()
address = models.TextField(default='', blank=True)
organisation = models.ForeignKey(Organisation, related_name='branches')
branch_details = models.OneToOneField('BranchDetails', related_name='branch')
class GemsBoughtHistory(CommonInfo):
gems_bought = models.PositiveIntegerField(null=True,default='',blank=True)
branch = models.ForeignKey(Branch, related_name='gems_in_branch')
I have organisation's instance, which I got by :
View:
organisation_id = self.kwargs['organisation_pk']
organisation = Organisation.objects.get(pk = organisation_id)
organisation_form = self.organisation_form(request.POST,instance=organisation)
if organisation_form.is_valid():
org = organisation_form.save()
gems = org.gems
gems_bought_history = GemsBoughtHistory.objects.create(gems_bought=gems,branch__organisation=organisation)
//gems_bought_history = GemsBoughtHistory.objects.create(gems_bought=gems,branch__organisation=org)
Now, while creating GemsBoughtHistory row, I get this error. "Invalid keyword argument".
How shall I traverse from branch to organisation in GemsBoughtHistory model??
Thank you in advance.
When using .create() you can't set values that do not reside on the created model. So, first create (or get an existing one from the DB) for the wanted organisation, then assign that branch to GemsBoughtHistory in .create().