开发者社区> 问答> 正文

Camelot(Python框架):指定备用EntityAdmin

使用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']

展开
收起
祖安文状元 2020-02-21 16:04:56 660 0
1 条回答
写回答
取消 提交回答
  • 我想到了。答案引起了人们的共鸣-我取消嵌套后,将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']
    
    2020-02-21 16:05:08
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载