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.)
精彩评论