如何从所有 ImageSpec 生成选择列表

发布于 2024-08-09 15:26:07 字数 650 浏览 2 评论 0原文

我想为继承自 imagekit.specs.ImageSpec 的所有规格生成一个选择列表。

这个想法是允许管理界面的用户选择要添加到图片中的 ImageSpec。

即:

class Display(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [ResizeDisplay,]

class SingleDisplay(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [SingleDisplayResize]

class Reflection(ImageSpec):
    increment_count = True
    processors = [ResizeDisplay, ReflectionProcessor]

class SingleDisplayReflection(ImageSpec):
    increment_count = True
    processors = [SingleDisplayResize, ReflectionProcessor]

结果出现下拉列表“显示、单显示、反射、单显示反射”

I want to generate a choicelist for all specs that inherit from imagekit.specs.ImageSpec.

The idea is to allow users of the admin interface to select an ImageSpec to add to a picture.

i.e:

class Display(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [ResizeDisplay,]

class SingleDisplay(ImageSpec):
    pre_cache = True
    increment_count = True
    processors = [SingleDisplayResize]

class Reflection(ImageSpec):
    increment_count = True
    processors = [ResizeDisplay, ReflectionProcessor]

class SingleDisplayReflection(ImageSpec):
    increment_count = True
    processors = [SingleDisplayResize, ReflectionProcessor]

results in a drop-down list "Display, Singledisplay, Reflection, Singledisplayreflection"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随心而道 2024-08-16 15:26:07

好吧,类似下面的内容将为您提供文件中定义的所有 ImageSpec 子类的列表:

def subclassfilter(x, baseclass):
    return x is not baseclass and isinstance(x, type) and issubclass(x, baseclass)

subclasses = [c for c in locals().values() if subclassfilter(c, ImageSpec)]

然后您可以从 subclasses 中每个类的 __name__ 属性生成选择列表代码> 列表。

Well, something like the following will get you a list of all the ImageSpec subclasses defined in the file:

def subclassfilter(x, baseclass):
    return x is not baseclass and isinstance(x, type) and issubclass(x, baseclass)

subclasses = [c for c in locals().values() if subclassfilter(c, ImageSpec)]

You could then generate the choices list from the __name__ attribute of each class in the subclasses list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文