2012-03-12 19 views
3

Camelot文檔說,它使用Elixir模型。由於SQLAlchemy已經包含了一段時間的declarative_base,我已經用它來代替Elixir用於其他應用程序。現在我想直接在Camelot中使用SQLAlchemy /聲明模型。Python Camelot與Elixir綁定了嗎?

有#2一個post,說柯萊特綁藥劑和使用不同的模型將是可能的,但它並沒有說如何。

柯萊特的原始model.py只有這樣的內容:

import camelot.types 
from camelot.model import metadata, Entity, Field, ManyToOne, OneToMany, Unicode, Date, Integer, using_options 
from camelot.view.elixir_admin import EntityAdmin 
from camelot.view.forms import * 

__metadata__ = metadata 

我說我SQLAlchemy的模式,改變了model.py這樣:

import camelot.types 
from camelot.model import metadata, Entity, Field, ManyToOne, OneToMany, Unicode, Date, using_options 
from camelot.view.elixir_admin import EntityAdmin 
from camelot.view.forms import * 

from sqlalchemy import Column, Integer, String 
from sqlalchemy.ext.declarative import declarative_base 

Base = declarative_base() 

__metadata__ = metadata 
Base = declarative_base() 

class Test(Base): 
    __tablename__ = "test" 
    id = Column(Integer, primary_key=True) 
    text = Column(String) 

它沒有工作。當我開始main.py時,我可以在邊欄中看到GUI和Test,但看不到任何行。這是回溯的尾巴:

File "/usr/lib/python2.6/dist-packages/camelot/view/elixir_admin.py", line 52, in get_query 
    return self.entity.query 
AttributeError: type object 'Test' has no attribute 'query' 

這是線46-52的elixir_admin.py代碼:

@model_function 
def get_query(self): 
    """:return: an sqlalchemy query for all the objects that should be 
    displayed in the table or the selection view. Overwrite this method to 
    change the default query, which selects all rows in the database. 
    """ 
    return self.entity.query 

如果此代碼導致了問題,我怎麼重寫方法來改變默認查詢,使其工作?

你怎麼能使用SQLAlchemy /聲明性模型在Camelot?

是否使用的是柯萊特的版本
+0

請你提供創造該問題的代碼?上面的代碼是 – Nilesh 2012-03-12 12:52:57

+1

。我添加了原來的'model.py'代碼,以便您可以看到差異。 – boadescriptor 2012-03-12 13:00:20

+0

是的,但我沒有找到任何聲明,你的代碼試圖訪問Test的'query'屬性。請發佈你的回溯。 – Nilesh 2012-03-12 13:06:32

回答

2

下面是關於使用聲明性定義柯萊特一個電影模型一些示例代碼,一些說明可以發現here

import sqlalchemy.types 
from sqlalchemy import Column 
from sqlalchemy.ext.declarative import (declarative_base, 
             _declarative_constructor) 

from camelot.admin.entity_admin import EntityAdmin 
from camelot.model import metadata 
import camelot.types 

from elixir import session 

class Entity(object): 

    def __init__(self, **kwargs): 
     _declarative_constructor(self, **kwargs) 
     session.add(self) 

Entity = declarative_base(cls = Entity, 
          metadata = metadata, 
          constructor = None) 

class Movie(Entity): 

    __tablename__ = 'movie' 

    id = Column(sqlalchemy.types.Integer, primary_key = True) 
    name = Column(sqlalchemy.types.Unicode(50), nullable = False) 
    cover = Column(camelot.types.Image(), nullable = True) 

    class Admin(EntityAdmin): 
     list_display = ['name'] 
     form_display = ['name', 'cover'] 
1

隨着柯萊特(30年11月12日)的最新版本,可以通過一些 黑客聲明使用。即將到來的版本將使它更容易,而在此之後,示例將被移植到聲明以及 。

+0

很高興知道它將在未來移植到聲明中!我在發行版的軟件包管理器中使用了10.07。任何方式來做出聲明式的工作? – boadescriptor 2012-03-12 15:02:21

+1

對於一個新項目,我建議不要使用10.07。因爲自定義操作在11.12更加靈活。 easy_install camelot要比嘗試一些解決方法容易得多。 – Erik 2012-03-12 15:19:10

+0

我無法使用easy_install版本,因爲它導致安裝的PyQT版本出現問題。 – boadescriptor 2012-03-12 15:29:31