These 2 methods are equivalent.
method 1
class X(object):
a = 1
method 2
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.
Alasdair
310k59 gold badges605 silver badges534 bronze badges
-
Seriously, it is poor form to come on here with questions of the form "How do I do x" without showing your attempt so far.Marcin– Marcin2012年02月14日 15:34:55 +00:00Commented Feb 14, 2012 at 15:34
-
I have tryed much things without result. Thanks for the comment.Youssef– Youssef2012年02月14日 15:42:32 +00:00Commented Feb 14, 2012 at 15:42
-
So, in what sense did this not work?Marcin– Marcin2012年02月14日 16:06:17 +00:00Commented Feb 14, 2012 at 16:06
-
4Do. Not. Do. This. All the future programmers who attempt to maintain your programs will hate you. What's wrong with doing things the proper way?S.Lott– S.Lott2012年02月14日 16:07:15 +00:00Commented Feb 14, 2012 at 16:07
3 Answers 3
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)
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Feb 14, 2012 at 15:24
Marcin
50.1k18 gold badges137 silver badges207 bronze badges
4 Comments
Marcin
@Youssef: How is that a problem? You already know how to create members and classes.
Youssef
Yes but the problem is how to create class dynamicaly with a meta data like model.
Youssef
stackoverflow.com/users/21640/marcin : Yes but the problem is how to create class dynamicaly with a meta data like model
Marcin
@Youssef: Your examples are nothing like the "normal" code you posted. That is your problem.
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)
answered Feb 14, 2012 at 17:48
Alasdair
310k59 gold badges605 silver badges534 bronze badges
1 Comment
Marcin
I'm pretty sure you haven't gained whatever OP is seeking if you still define
Meta using normal syntax. That said, he should be able to work for himself how to do that too.lang-py