Tuesday, June 3, 2014

Re: Change help_text of a field in a derived model

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 
    __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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ae04dd92-3e76-49cd-a7d7-562753835a42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment