[Django]-Hidden field in Django Model

57👍

from the docs on Using a subset of fields on the form:

Set editable=False on the model field. As a result, any form created from the model via ModelForm will not include that field.

72👍

If you have access to the template you could render it as a hidden field with the following code:

{{ form.field_name.as_hidden }}

instead of the standard:

{{ form.field_name }}

4👍

You could define a custom model field subclass and override the formfield() method to return a field with a HiddenInput widget. See the documentation for custom fields.

1👍

Though you mentioned that you cannot use exclusion in your case, I think others who come across this answer (like myself, based on the title) may find it helpful.

It is possible to selectively hide fields using exclude in ModelAdmin, here is a snippet from something I’m working on:

class ItemsAdmin(admin.ModelAdmin):
    form = ItemsForm
    actions = None
    list_display = ('item_id', 'item_type', 'item_title', 'item_size', 'item_color',)
    search_fields = ('item_id', 'item_title',)
    inlines = [ImageInline,]
    readonly_fields = ('disable_add_date','disable_remove_date',)
    exclude = ('add_date', 'remove_date',)
    ###.............

Leave a comment