我在显示相关表格时会出现以下错误。
sqlalchemy.orm.exc.UnmappedInstanceError UnmappedInstanceError:Class 'sqlalchemy.util._collections.result'未映射
这是models.py
class Project(db.Model):
__tablename__ = 'projects'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(175), unique=True, nullable=False)
description = db.Column(db.Text)
#---------------------------
region = db.Column(db.Integer, db.ForeignKey('regions.id'))
district = db.Column(db.Integer, db.ForeignKey('districts.id'))
subdistrict = db.Column(db.Integer, db.ForeignKey('subdistricts.id'))
village = db.Column(db.Integer, db.ForeignKey('villages.id'))
#------------------------------------
baseline = db.Column(db.Text())
performance_indicator = db.Column(db.Text)
budget = db.Column(db.Float())
author = db.Column(db.Integer, db.ForeignKey('users.id'))
posted_date = db.Column(db.DateTime)
start_date = db.Column(db.DateTime)
est_completion = db.Column(db.DateTime)
mark_complete = db.Column(db.Boolean, default=False)
activities = db.relationship('Activity', backref='projects', cascade='all, delete-orphan', lazy='dynamic')
remarks = db.relationship('Remark', backref='projects', cascade='all, delete-orphan', lazy='dynamic')
beneficiary = db.Column(db.Integer, db.ForeignKey('beneficiaries.id'))
sector = db.Column(db.Integer, db.ForeignKey('sectors.id'))
media = db.relationship('Media', secondary=projects_media, backref='projects')
def __init__(self, id, title, description, baseline, performance_indicator, budget, remark, remark_author, author, posted_date, start_date, est_completion, mark_complete):
self.id = id
self.title = title
self.description = description
self.baseline = baseline
self.performance_indicator = performance_indicator
self.budget = budget
self.author = author
self.posted_date = posted_date
self.start_date = start_date
self.est_completion = est_completion
self.mark_complete = mark_complete
forms.py
from flask.ext.wtf.file import FileRequired, FileAllowed, FileField
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from project.location.models import Region, District, Subdistrict, Village
from flask.ext.uploads import UploadSet, IMAGES, DOCUMENTS
from flask_wtf import Form
from wtforms.validators import DataRequired, NumberRange
from . models import Sector, Project, Activity
from project import db
from project.crc.models import CrC
from wtforms import StringField, TextAreaField, validators, SubmitField, SelectField, IntegerField, DateTimeField, \
RadioField, BooleanField, DecimalField, DateField, FileField
def sector_lists():
return db.session.query(Sector.name)
def region_lists():
return db.session.query(Region.region)
def district_lists():
return db.session.query(District.district)
def sub_districts():
return db.session.query(Subdistrict.subdistrict)
def villages_lists():
return db.session.query(Village.village)
def project_lists():
return db.session.query(Project.title)
def activity_lists():
return db.session.query(Activity.title)
images = UploadSet('images', IMAGES)
class ProjectForm(Form):
title = StringField('Project Name', validators=[DataRequired()])
description = TextAreaField('Project Details', validators=[DataRequired()])
sector = QuerySelectField(get_label='Service Sector', query_factory=sector_lists)
regions = QuerySelectField(get_label='Regions', query_factory=region_lists)
district = QuerySelectField(get_label='Regions', query_factory=district_lists)
subdistrict = QuerySelectField(get_label='Subdistrict', query_factory=sub_districts)
village = QuerySelectField(get_label='Village', query_factory=villages_lists)
baseline = TextAreaField('Baseline')
performance_indicator = TextAreaField('Performance Indicator')
budget = DecimalField("Planned Budget, places'2'")
started = DateTimeField('Project Started Date')
estimated_completion = DateField('Estimated Completion date', format='%Y-%m-%d')
completed = BooleanField('Completed?', default=False)
media_gallery = FileField('image', validators=[
FileRequired(),
FileAllowed(images, "Image Only")
])
最后是我的views.py
@app.route('/add', methods=['GET', 'POST'])
def deliverables():
forms = ProjectForm()
if request.method == 'POST' and forms.validate_on_submit():
pmodel = Project(title=forms.title.data,description=forms.description.data, baseline=forms.baseline.data, performance_indicator=forms.performance_indicator.data, budget=forms.budget.data, author='mainuser', posted_date=datetime.datetime.utcnow(), start_date=forms.started.data, est_completion=forms.estimated_completion.data, mark_complete=forms.completed.data)
db.session.add(pmodel)
db.session.commit()
return render_template('front/deliverables_edit.html', forms=forms)
答案 0 :(得分:0)
query_factory
回调应该返回完整的实体,而不是列
def sector_lists():
return db.session.query(Sector)
def region_lists():
return db.session.query(Region)
def district_lists():
return db.session.query(District)
答案 1 :(得分:0)
作为您的代码:
def sector_lists():
return db.session.query(Sector.name)
您无法将列返回到QuerySelectField的query_factory(必须返回查询),您已在QuerySelectField的get_label中指定:
sector = QuerySelectField(get_label='Service Sector',query_factory=sector_lists)
这就够了。您必须更改功能sector_lists
:
def sector_lists():
return db.session.query(Sector)