开发者

python creating a class at runtime containing another class

开发者 https://www.devze.com 2023-02-26 05:59 出处:网络
I wanted to generate a class at runtime like the following: from google.appengine.ext.db import djangoforms

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

精彩评论

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