python - ModelForm's metaclass prevents adding of extra fields -



python - ModelForm's metaclass prevents adding of extra fields -

i'm writing mixin class dynamically add together fields modelform sublasses it.

the new fields generated in mixin's __init__ method. see bellow:

mixin class code (strongly shortened & simplified):

class foreignfieldsmixin(object): # … def __init__(self, *args, **kwargs): # … instance = kwargs.get('instance') self.fields[extra_field_name] = fields_for_model(fmodel, (fldname,)) # ^^^^^^ here new field instance added self.fields[extra_field_name].initial = 'some value' # ^^^^^^ here set inital value new field # …

however form's class still refuses list new field name in meta.fields:

class contactadminform(foreignfieldsmixin, forms.modelform): # … class meta: model = contact fields = ('title', 'extra_field_name',)

and fails next exception

file "../lib/python2.7/site-packages/django/forms/models.py", line 215, in __new__ raise fielderror(message) fielderror: unknown field(s) (extra_field_name) specified contact

i suspect modelform's metaclass (or of of ancestors) @ time of class definition ie. before classes instantiated raises exception not yet knows new field names added.

i not know if i'm taking wrong side documentation rather sparse in area. avoid monkey-patching of modelform's metaclass.

is there other way how simulate basic in-code fields definition satisfy django's class model metaclasses protectors dynamically applicable class/function ?

you don't want or need specify fields in fields tuple. you're adding them manually in __init__, putting them in fields irrelevant.

python django django-forms metaclass

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -