Reputation: 169
These 2 methods are equivalent.
class X(object):
a = 1
X = type('X', (object,), dict(a=1))
I want to know what is the equivalent of :
class ObjectTable(tables.ModelTable):
id = tables.Column(sortable=False, visible=False)
societe = tables.Column(sortable=False, visible=False)
class Meta:
model = models.get_model('core', "Fournisseur")
I tried this but don't work :
ObjectTable=type('ObjectTable',(tables.ModelTable,),dict(model=myModel))
ObjectTable=type('ObjectTable',(tables.ModelTable,),dict(meta.model=myModel))
ObjectTable=type('ObjectTable',(tables.ModelTable,),dict(meta=myModel))
Thanks.
Upvotes: 0
Views: 181
Reputation: 169
This is the solution :
def CreateForm(for_model, request=None, instance=None, user=None):
class _StateMachineBaseModelForm(ModelForm):
class Meta:
model = for_model
exclude = ('societe',)
def __init__(self, *args, **kwargs):
super(_StateMachineBaseModelForm, self).__init__(*args, **kwargs)
try:
if user:
self.fields['banque'].queryset = Banque.objects.filter(pays=user.get_profile().societe.pays)
except:
pass
if for_model: return _StateMachineBaseModelForm(request, instance=instance)
Upvotes: 1
Reputation: 308799
Before we start, I agree with S. Lott in the comments. You almost certainly don't want to do this.
Here goes:
# You can't create an old-style class with type,
# so define a function that creates one.
def make_meta_class():
class Meta:
model = models.get_model('core', "Fournisseur")
return Meta
# Create the dictionary (remember to include the fields as well as the meta class)
d=dict(,
Meta=make_meta_class(),
id=tables.Column(sortable=False, visible=False)
societe=tables.Column(sortable=False, visible=False)
)
# Create the class dynamically
ObjectTable=type('ObjectTable', (tables.ModelTable,), d)
Upvotes: 0
Reputation: 49816
It's the exact same thing with the values that you find in your django example. Try it for yourself.
All of your examples "do not work" as you put it, because (a) you don't create any fields other than meta
(b) that should be spelled Meta
(c) the value of Meta
should be an (old style) class.
Upvotes: 0