2017-08-08 50 views
0

試圖添加一個自定義批處理動作,但我不斷收到此錯誤,我已將我的導入添加到代碼中以顯示我正在導入的內容,此文件是分開我的模型文件,這是我的views.py文件AttributeError:type object'Page'has no attribute'query'

Views.py

import flask_admin as admin 
import textwrap 
from flask_admin.actions import action 
from flask_admin.contrib import sqla 
from flask_admin.contrib.sqla import ModelView 
from flask_admin.contrib.sqla.filters import FilterEqual 
from app.models import FileRegister, FileRegisterStatus 
from app import app, db, models 


class PageView(ModelView): 
    @action("active", "Active", 'Are you sure you want to active selected pages?') 
def action_active(self, ids): 
    try: 
     query = Page.query.filter(Page.id.in_(ids)) 

     count = 0 
     for page in query.all(): 
      page.is_active = True  
      count += 1 

     flash(ngettext('Page was successfully activated.', 
         '%(count)s pages were successfully activated.', 
         count, 
         count=count)) 
    except Exception as ex: 
     if not self.handle_view_exception(ex): 
      raise 

     flash(gettext('Failed to active pages. %(error)s', error=str(ex)), 'error') 

models.py

from sqlalchemy.orm import relationship 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy import Column, Integer, String, Boolean, TIMESTAMP, ForeignKey 
import datetime 

Base = declarative_base() 


class Page(Base): 
    __tablename__ = 'page' 
    id = Column(Integer, primary_key=True, nullable=False, autoincrement=True) 
    page_name= Column(String(500), index=True, nullable=False) 
    is_page_active= Column(Boolean()) 

    def __init__(self, page_name, is_page_active): 
     self.page_name= page_name 
     self.is_page_active= is_page_active 

    def __unicode__(self): 
     return self.page_name 

這是Traceback(最近調用的最後一個)我在運行代碼後得到的,我認爲它是因爲我在映射類而不是會話上調用了query()。

Traceback (most recent call last): 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1997, in __call__ 
    return self.wsgi_app(environ, start_response) 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1985, in wsgi_app 
    response = self.handle_exception(e) 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1540, in handle_exception 
    reraise(exc_type, exc_value, tb) 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1982, in wsgi_app 
    response = self.full_dispatch_request() 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1517, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "C:\page\env\lib\site-packages\flask\app.py", line 1598, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "C:\page\flask_admin\base.py", line 69, in inner 
    return self._run_view(f, *args, **kwargs) 
    File "C:\page\flask_admin\base.py", line 368, in _run_view 
    return fn(self, *args, **kwargs) 
    File "c:\page\flask_admin\model\base.py", line 2145, in action_view 
    return self.handle_action() 
    File "C:\page\flask_admin\actions.py", line 117, in handle_action 
    response = handler[0](ids) 
    File "C:\page\app\views.py", line 28, in action_active 
    if not self.handle_view_exception(ex): 
    File "c:\page\flask_admin\contrib\sqla\view.py", line 1060, in handle_view_exception 
    return super(ModelView, self).handle_view_exception(exc) 
    File "C:\page\app\views.py", line 16, in action_active 
    query = Page.query.filter(Page.active_ind.in_(ids)) 
AttributeError: type object 'Page' has no attribute 'query' 
+0

沒有關於正在使用的類/包的知識,嘗試'query = Page.query()。filter(Page.id.in_(ids))'? –

+0

@RobMurray我已經添加了我的包,我在我的模型類中使用了一個頁面模型 – ghost

+0

我相信,既然您使用的是聲明性模型,您希望創建一個會話對象並根據模型執行查詢。 'session.query(Page).filter_by(Page.id.in_(ids)''')的一些東西''。在SQLAlchemy頁面查看[here]部分(http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#adding-and-updating-objects) – shiv

回答

0

因爲我使用的是聲明模型,所以這是修復。這進入models.py文件

from sqlalchemy import create_engine 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.orm import scoped_session, sessionmaker  

engine = create_engine('databaselinkgoeshere', convert_unicode=True) 
    db_session = scoped_session(sessionmaker(autocommit=False, 
              autoflush=False, 
              bind=engine)) 

Base = declarative_base() 
Base.query = db_session.query_property() 
相關問題