Niklas R
Niklas R

Reputation: 16860

Django, define field to be hidden in every form

I have an abstract model NamedEntry. It has a field body_raw which contains the text as it was input by the user. The body field contains the converted (from markdown to html) cache for the body_raw field.

The body field should be hidden by default everywhere, on any form it might be used with. Is there something like body = models.TextField(blank=True, hidden=True)?

Upvotes: 1

Views: 705

Answers (1)

awesoon
awesoon

Reputation: 33651

You can write your own Field, which will use widget with attribute hidden, for example:

class HiddenTextField(models.TextField):
    def formfield(self, **kwargs):
        defaults = {'widget': widgets.Textarea({'hidden': ''})}
        defaults.update(kwargs)
        return super(HiddenTextField, self).formfield(**defaults)

And just replace TextField to HiddenTextField in your model(s)

Upvotes: 2

Related Questions