gpt4 book ai didi

python - Python Camelot 是否与 Elixir 相关?

转载 作者:行者123 更新时间:2023-11-28 16:49:17 25 4
gpt4 key购买 nike

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

有一个post在 Stackoverflow 上说 Camelot 与 Elixir 绑定(bind)并且可以使用不同的模型但它没有说明如何。

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/声明式模型?

最佳答案

这里是一些使用声明式为 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']

关于python - Python Camelot 是否与 Elixir 相关?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9666303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com