python类中的table_尝试动态添加Python类的方法(即django-tables2'Table')

So for a Django project, I would really like to be able to generate and display tables (not based on querysets) dynamically without needing to know the contents or schema beforehand.

It looks like the django-tables2 app provides nice functionality for rendering tables, but it requires that you either explicitly declare column names by declaring attributes on a custom-defined Table subclass or else provide a model for it infer the columns.

I.e, to use a column named "name", you'd do:

class NameTable(tables.Table):

name = tables.Column()

The Tables class does not provide a method for adding columns post-facto because, from reading the source, it seems to use a metaclass that sweeps the class attributes on __new__ and locks them in.

It seemed like very simple metaprogramming would be an elegant solution. I defined a basic class factory that accepts column names are arguments:

def define_table(columns):

class klass(tables.Table): pass

for col in columns:

setattr(klass, col, tables.Column())

return klass

Sadly this does not work. If I run `

x = define_table(["foo", "bar"])(data)

x.foo

x.bar

I get back:

But if I list the columns:

print x.base_columns

I get back nothing i.e. {}

I realize that there are probably simpler solutions (e.g. just bite the bullet and define every possible data configuration in code, or don't use django-tables2 and roll my own), but I am now treating this as an opportunity to learn more about meta programming, so I would really like to make this work this way.

Any idea what I'm wrong doing wrong? My theory is that the __new__ method (which is redefined in the metaclass Table uses) is getting invoked when klass is defined rather than when it's instantiated, so by the time I tack on the attributes it's too late. But that violates my understanding of when __new__ should happen. Otherwise, I'm struggling to understand how the metaclass __new__ can tell the difference between defined-in-code attributes vs. dynamically defined ones.

Thanks!

解决方案

You're on the right track here, but instead of creating a barebones class and adding attributes to it, you should use the type() built-in function. The reason it's not working the way you're trying, is because the metaclass has already done its work.

Using type() allows you to construct a new class with your own attributes, while setting the base class. Meaning - you get to describe the fields you want as a blueprint to your class, allowing the Tables metaclass to take over after your definition.

Here's an example of using type() with django. I've used this myself for my own project (with some slight variations) but it should give you a nice place to start from, considering you're already almost there.

def define_table(columns):

attrs = dict((c, tables.Column()) for c in columns)

klass = type('DynamicTable', (tables.Table,), attrs)

return klass

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种高级编程语言,而Django是一个使用Python编写的Web框架。它们可以一起使用来开发功能强大的Web应用程序。 Bootstrap是一个开源的前端框架,它提供了一套美观和响应式的页面设计元素和样式。它的主要目标是简化开发者在Web上创建美观和易于使用的页面的工作。 Bootstrap-Table是基于Bootstrap框架的一个强大的jQuery表格插件。它提供了丰富的功能和选项,使开发者能够很容易地在Web应用程序创建和管理数据表格。Bootstrap-Table允许开发者使用少量的HTML和JavaScript代码来自定义和配置表格的样式和功能。 在Python Django使用Bootstrap-Table可以帮助我们更轻松地创建和管理数据表格。我们可以将Bootstrap-TableDjango的模型和视图结合使用,从数据库获取数据并在Web应用程序显示它们。通过使用Bootstrap-Table的功能,我们可以对表格进行排序、分页、筛选等操作。 使用Bootstrap-Table的一个例子是在Django的视图使用它来显示查询结果。我们可以通过在模板引入Bootstrap-Table的样式和脚本文件,然后在视图将查询结果传递给模板。在模板,我们可以使用Bootstrap-Table的数据属性和选项来定义表格的样式和功能。最后,我们可以使用Bootstrap-Table的JavaScript方法来初始化和渲染表格,并在页面上显示查询结果。 总而言之,Python Django与Bootstrap-Table的结合可以让开发者更轻松地创建和管理数据表格,从而为Web应用程序提供更好的用户体验和功能。这是一个强大的组合,可以帮助我们快速开发高效的Web应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值