开发者

python creating a class at runtime containing another class

I wanted to generate a class at runtime like the following:

from google.appengine.ext.db import djangoforms

class TestForm(ConsumerForm):
  class Meta:
    model = Consumer

I can use

form_model = type("TestForm", (djangoforms.ModelForm,), {})

to creat开发者_运维百科e the TestForm class but I'm not sure how to create the Meta class inside it?


Create it in the same manner as TestForm and put it into TesForm's dictionary:

Meta = type("Meta", (), {"model": Consumer})
TestForm = type("TestForm", (djangoforms.ModelForm,), {"Meta": Meta})

(Disclaimer: I'd usually avoid dynamically creating classes.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜