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 }}
- [Django]-Django Rest Framework writable nested serializers
- [Django]-How do I JSON serialize a Python dictionary?
- [Django]-Where to store secret keys DJANGO
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.
- [Django]-Check if OneToOneField is None in Django
- [Django]-How to serve media files on Django production environment?
- [Django]-Get class name for empty queryset in django
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',)
###.............
- [Django]-Is it possible to decorate include(…) in django urls with login_required?
- [Django]-Django: using blocks in included templates
- [Django]-How do I add a custom column with a hyperlink in the django admin interface?