0

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
asked Feb 14, 2012 at 15:19
4
  • 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. Commented Feb 14, 2012 at 15:34
  • I have tryed much things without result. Thanks for the comment. Commented Feb 14, 2012 at 15:42
  • So, in what sense did this not work? Commented Feb 14, 2012 at 16:06
  • 4
    Do. 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? Commented Feb 14, 2012 at 16:07

3 Answers 3

1

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)
answered Feb 18, 2012 at 21:43
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

@Youssef: How is that a problem? You already know how to create members and classes.
Yes but the problem is how to create class dynamicaly with a meta data like model.
stackoverflow.com/users/21640/marcin : Yes but the problem is how to create class dynamicaly with a meta data like model
@Youssef: Your examples are nothing like the "normal" code you posted. That is your problem.
0

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

1 Comment

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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.