Change help_text of a field in a derived model

51 views
Skip to first unread message

Vladimir Chukharev

unread,
Jun 2, 2014, 5:17:01 PM6/2/14
to django...@googlegroups.com
Hi,
I a have a question about changing help_text in a model derived from another model. In my case, all of the models use the same form, generated from model.

Few classes share the same TextField. But the meaning of the field is different in different models. The following is a simplified version to illustrate the question, errors are probably added ;-).

#models.py:
from django.utils.html import format_html
import django.db.models.options as options

options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('help_texts',)

class Fact(models.Model):
"Model with plain text"
name = models.TextField(help_text="Simple text, it will be escaped in html")

def __unicode__(self):
return self.name

class ReST(Fact):
"""Derived model with ReStructured Text, it will be converted to html and marked safe.
This model needs a different self.name.help_text, but assigning to it in 
__init__ is not possible (self.name is None when __init__ is running)
"""
def __unicode__(self):
return format_html(rest_to_html(self.name))

class Meta:
help_texts = {'name': 'You can use ReST formating here.'}

#views.py:
class BaseCreateView(CreateView):
def get_form_class(self):
model = class_by_name(self.kwargs['fmname'])
opts = model._meta
if hasattr(opts, 'help_texts'):
for fl in opts.concrete_fields:
if fl.name in opts.help_txt:
fl.help_text = opts.help_txt[fl.name]
form = model_forms.modelform_factory(model)
return form


The above use of Meta results in a situation, that with one class more (derived from ReST), its help_text value depends on history of using forms for ReST and Fact...

My question: is there a way to change help_text of a model field defined in a parent class from within a derived model class?

Vladimir Chukharev

unread,
Jun 3, 2014, 8:32:43 AM6/3/14
to django...@googlegroups.com
Hm... I fought with indentations in Opera in vain. Everything is lost, though at the time of sending it it looked fine. Try again, with FF.

#models.py:
from django.utils.html import format_html
import django.db.models.options as options

options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('help_texts',)

class Fact(models.Model):
    "Model with plain text"
    name = models.TextField(help_text="Simple text, it will be escaped in html")

    def __unicode__(self):
        return self.name

class ReST(Fact):
    """Derived model with ReStructured Text
    This model needs different self.name.help_text, but assigning to it in 

Vladimir Chukharev

unread,
Jun 19, 2014, 5:18:39 AM6/19/14
to django...@googlegroups.com
Answering my own question. No, there is no ready way to change help_text in model.
The proposed solution is almost correct, just the get_form_class() function should
be defined both in CreateViiew- and UpdateView-derived classes and be like the
following.

    def get_form_class(self):
        'Make a form from a class'


        model = class_by_name(self.kwargs['fmname'])
        form = model_forms.modelform_factory(model)

        # change help_text for fields in derived class, i.e in ReSt class and
        # derived from it
        class_list = [model]
        class_list.extend(model._meta.get_parent_list())
        for mod in class_list:
            opts = mod._meta
            if hasattr(opts, 'help_texts'):
                for key, value in opts.help_texts.iteritems():
                    form.base_fields[key].help_text = value
                return form

        return form

The stuff added for Meta help_texts to work is between empty lines.
Reply all
Reply to author
Forward
0 new messages