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
Related
This is the model :
class Requirement(models.Model):
name = models.CharField(max_length=100)
user = models.ForeignKey(
User,on_delete=models.CASCADE, related_name = 'user'
)
assigned_user = models.ForeignKey(
User,related_name = "assigned",on_delete=models.CASCADE,
)
I am running this query:
requirementsOb = Requirement.objects.filter(user = currentUser)
Where currentUser is logged in user. The result returns multiple requriements. I also want to get all user related data. How can i get user related data only for assigned_user
You can try like this:
current_user = request.user
requirements = current_user.user.all() | current_user.assigned.all()
Or you can try this approach:
requirementsOb = Requirement.objects.filter(Q(user = currentUser) | Q(assigned_user=currentUser))
If you want to get user data from requirements, you can try:
for r in requirementsOb:
r.user
r.assigned_user
If you want to get only the first requirement from the requirementsOb, then try like this:
requirement = requirementsOb.first() # or .last() for last object
requirement.user
requirement.assigned_user
More information can be found in documentation.
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.
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.
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().
I am trying a record in this table but failed. Please someone show me an example , How do i create a record in this table using ApplicationStage.objects.create(...............)
class ApplicationStage(Base):
stage_id = models.AutoField(primary_key=True)
leave_id = models.ForeignKey(Leave)
auth_id = models.ForeignKey(ApproveAuthority)
approve_status_id = models.ForeignKey(Status)
approve_total_day = models.PositiveIntegerField(default=0)
comment = models.TextField()
approved_time = models.DateTimeField(auto_now_add=True)
is_livedate = models.CharField(choices=(("yes","Yes"),
("no","No"),
),max_length=15)
Navigate to the directory to your application and run:
python manage.py shell
Then import model:
from your_app_name.models import ApplicationStage
Then use the following command to create a record:
a = ApplicationStage(stage_id=1, leave_id=1, auth_id .......... and so on)
a.save()