Python Camelot是否与Elixir绑定?

时间:2012-03-12 11:36:21

标签: python sqlalchemy crud python-elixir

Camelot的文档说它使用Elixir模型。由于SQLAlchemy已经包含了一段时间的declarative_base,我已经使用它而不是Elixir用于另一个应用程序。现在我想直接在Camelot中使用SQLAlchemy /声明模型。

Stackoverflow上有post表示Camelot 与Elixir绑定,并且使用不同的模型是可能的,但它没有说明如何。

Camelot的原始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

如果此代码导致问题,如何覆盖该方法以更改默认查询以使其有效?

如何在Camelot中使用SQLAlchemy /声明模型?

2 个答案:

答案 0 :(得分:2)

以下是使用Declarative为Camelot定义Movie模型的示例代码,可以找到一些解释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 :(得分:1)

您使用的是哪个版本的Camelot?

使用当前版本的Camelot(11.12.30),可以通过一些使用Declarative 黑客。即将推出的版本将更容易,而在此之后,示例将是 移植到Declarative也是如此。