Cómo agregar readonly en línea en django admin
Estoy usando django 1.4 y tengo un campo many2many, por lo que al crear el sitio de administración quería agregar este campo como un inline, aquí hay algo de código:
class SummaryInline(admin.TabularInline):
model = ParserError.summaries.through
class MyClassAdmin(admin.ModelAdmin):
list_display = ('classifier', 'name', 'err_count', 'supported')
fields = ('classifier', 'name', 'err_count', 'err_classifier', 'supported')
inlines = (SummaryInline,)
readonly_fields = ('classifier', 'err_count')
Así que mi pregunta es, ¿cómo puedo hacer que el campo en línea sea de solo lectura?
28
3 answers
Después de un tiempo de intentar encontrar el nombre me di cuenta gracias a esta respuesta , así que revisé los nombres en self.opts.local_fields
y encontré el nombre de la tabla central y lo agregué a readonly_fields
, ajuste can_delete
a False.
class SummaryInline(admin.TabularInline):
model = ParserError.summaries.through
readonly_fields = ('myclasssummary',)
can_delete = False
Bastante simple, pero me llevó un tiempo, así que me di cuenta de que era una buena idea agregarlo aquí.
42
Author: Hassek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:02:14
Además, si no desea la capacidad de agregar/eliminar las filas, puede agregar estas definiciones.
def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False
21
Author: Keval Prabhu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-08 08:41:53
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-08 08:41:53
Gracias Keval Prabhu
class UnitsInline(admin.TabularInline):
model = Units
extra = 0
verbose_name = 'Units'
verbose_name_plural = 'Units of company'
**def has_add_permission(self, request, obj=None):
return False
def has_delete_permission(self, request, obj=None):
return False**
1
Author: Muhammad Hafid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-28 09:56:30
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-09-28 09:56:30