I wanted to add extra argument to model.Field
base class to
use later without defining field's extra options again in Form
or ModelForm
class.
And I can add extra argument with:
class Poll(models.Model):
question = models.CharField(max_length=200,
extra= { 'widget': 'xw',
'admin_list_order': 3 })
pub_date = models.DateTimeField('date published')
I can access extra arg with:
(InteractiveConsole)
>>> from polls.models import Poll
>>> Poll._meta.fields[1].extra
{'widget': 'xw', 'admin_list_order': 3}
>>>
Is this an appropriate, Pythonic way to add an extra argument?
from django.db import models
# save old __init__ function of Field class
__oInit = models.Field.__init__
# define a new one to accept 'extra' arg
def __xinit__(self, *args, **kwargs):
#chek if extra arg exist
if kwargs.has_key('extra'):
self.extra = kwargs.pop('extra')
#call real __init__
__oInit(self, *args, **kwargs)
# replace init with new one
models.Field.__init__ = __xinit__