36👍
✅
You can create classes on the fly by calling the type
built-in, passing appropriate arguments along, like:
CommentForm = type("CommentForm", (Form,), {
'name': forms.CharField(),
...
})
It works with new-style classes. I am not sure, whether this would also work with old-style classes.
👤Dirk
14👍
Classes can be defined almost anywhere.
def newclass(val):
class C(object):
def __str__(self):
return str(val)
return C
MyClass = newclass(5)
m = MyClass()
print str(m)
- [Django]-How do you Require Login for Media Files in Django
- [Django]-Project Name vs Application Name in Django
- [Django]-How to change Django Admin Custom list field label?
Source:stackexchange.com