tags:

views:

33

answers:

1

I have a custom model fields, that can have 'chain' argument.


from django.db import models

class ChainField(object):
    def __init__(self, *args, **kwargs):
        chain = kwargs.get('chain', False)
        if chain:
            self.chain = chain
            del kwargs['chain']

        super(self.__class__.__mro__[2], self).__init__(*args, **kwargs)

class DateTimeField(ChainField, models.DateTimeField):
    pass

And now the question: how I can automatically pass 'chain' argument of model field to widget class when initializing ModelForm? I neen that in html it become 'class="chainxxx"' attribute of form field.

A: 

Override __init__ of the ModelForm like this:

class MyClass(ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyClass, self).__init__(*args, **kwargs)

        chain_value = self.fields['name_of_the_field'].chain
        self.fields['name_of_the_field'].widget = CustomWidget(chain=chain_value)
sorki