使用Camelot框架,可以使用嵌套类(从EntityAdmin的子类)定义模型(从Entity的子类),该嵌套的类定义各种gui属性,例如布局和其他小部件。该文档表明可以定义多个EntityAdmin,然后由调用模型指定:
管理员 对于关系字段,请指定用于可视化关系另一端的管理类。默认为目标类的默认管理类。这可用于使one2many小部件内的表视图看起来与同一对象的默认表视图不同。 Camelot:“字段属性”手册页
我似乎无法弄清楚所需的语法。作为案例研究,有人可以帮助我通过包装中的“ camelot-example”来解决该问题吗?(Camelot 13.04.13,Python 2.7.6)
这是我的示例代码:
from sqlalchemy.schema import Column
from sqlalchemy.types import Unicode, Integer
from camelot.admin.entity_admin import EntityAdmin
from camelot.core.orm import Entity, ManyToOne, OneToMany
import camelot.types
class Company(Entity):
__tablename__ = 'company'
name = Column(Unicode())
city = Column(Unicode())
employees = OneToMany('Employee')
def __unicode__(self):
return self.name or ''
class Admin(EntityAdmin):
verbose_name = 'Company'
verbose_name_plural = 'Companies'
list_display = ['name', 'city', 'employees']
field_attributes = {'employees': {'create_inline': True},
'admin': 'AlternativeAdmin'}
class Employee(Entity):
__tablename__ = "employee"
name = Column(Unicode())
age = Column(Integer())
company = ManyToOne('Company')
def __unicode__(self):
return self.name or ''
class Admin(EntityAdmin):
verbose_name = 'Employee'
list_display = ['name', 'age', 'company']
class AlternativeAdmin(EntityAdmin):
verbose_name = 'Employee'
list_display = ['name']
我想到了。答案引起了人们的共鸣-我取消嵌套后,将AlternativeAdmin类定义保留在下面的位置。一旦将其移到上方,它就可以正常工作。
这是我的问题示例的完整固定版本:
from sqlalchemy.schema import Column
from sqlalchemy.types import Unicode, Integer
from camelot.admin.entity_admin import EntityAdmin
from camelot.core.orm import Entity, ManyToOne, OneToMany
import camelot.types
class AlternativeAdmin(EntityAdmin):
verbose_name = 'Employee'
list_display = ['name']
class Company(Entity):
__tablename__ = 'company'
name = Column(Unicode())
city = Column(Unicode())
employees = OneToMany('Employee')
def __unicode__(self):
return self.name or ''
class Admin(EntityAdmin):
verbose_name = 'Company'
verbose_name_plural = 'Companies'
list_display = ['name', 'city', 'employees']
field_attributes = {'employees': {'create_inline': True,
'admin': AlternativeAdmin}}
class Employee(Entity):
__tablename__ = "employee"
name = Column(Unicode())
age = Column(Integer())
company = ManyToOne('Company')
def __unicode__(self):
return self.name or ''
class Admin(EntityAdmin):
verbose_name = 'Employee'
list_display = ['name', 'age', 'company']
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。