14π
The Django admin does not support hidden fields yet. There is an open ticket for that: https://code.djangoproject.com/ticket/11277
However, there are workarounds that donβt require jQuery. The admin forms are rendered using admin/includes/fieldset.html
. If you override this template, you can inject a CSS class to denote the row for hiding:
<div class="form-row
{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}
{% for field in line %} {{ field.field.name }}
{% if field.field.is_hidden %} has-hidden-field{% endif %} # add this part
{% endfor %}">
this is actually a single line in the file, Iβve expanded it to make it more readable.
( Neat detail: for an StackedInline/TabularInline objects, you can specify the template as variable in Python code. )
Next, you can hide that row in your CSS:
.form-row.has-hidden-field {
display: none;
}
Which you can load via your admin page:
{% block extrastyle %}{{ block.super }}
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}mysite/admin.css" />{% endblock %}
or by using the media definition in the modeladmin:
class Media:
css = {'all': ('mysite/admin.css',)
2π
In the template, while displaying
{% for field in form.visible_fields %}
{{ field.label_tag }} : {{ field }}
{% endfor %}
It will hide the hidden field labels.
- Custom list_editable field in django admin change list, which doesn't correspond directly to a model field
- Do RESTful service parameters have to be discoverable?
- Can you use Python for both front end and back end using Django framework?
2π
I found it quite handy to use the βclassesβ attribute in fieldsets to hide fields but still leave them in a request. In your model admin you can write
fieldsets = [
('Visible Fields',
{'fields': [('name', 'comment'),]}),
('Collapsable Fields',
{'fields': [('rare_property',)],'classes': ['collapse']}),
('Hidden Fields',
{'fields': [('magic_property',)],'classes': ['hidden']}),
]
0π
Youβre giving ModelForm in your example, not ModelAdmin you should be using for admin site.
Anyway, method of excluding some field is the same: specify it in exclude
property:
class OutForm(ModelForm):
class Meta:
exclude = ["reply_to"]
or
class OutAdmin(ModelAdmin):
exclude = ["reply_to"]
See Django documentation for details: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/
- Scheduling a regular event: Cron/Cron alternatives (including Celery)
- 'AnonymousUser' object is not iterable
- Blank label_suffix across entire Django project
0π
Try adding label=ββ to your ModelChoiceField to make the label an empty string:
reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), label="", widget=forms.HiddenInput)
- Django admin filter using F() expressions
- Custom list_editable field in django admin change list, which doesn't correspond directly to a model field
- Django β import error: no module named views
0π
It seems that in Django 3.2 the HiddenInput widget is enough to hide the entire row from the form. I have tried this both via custom admin form and via the formfield_override property. The div with class "form-row" containing the hidden field gets additional class "hidden" and is not shown.
- Django, ReportLab PDF Generation attached to an email
- Who generates the default page of django welcome page?
- How to make django messages StackOverflow style?
- Xhtml2pdf ImportError β Django