Reading fields in django admin / inline

I use this snippet to display multiple fields in my admin backend as read-only, but as noted in the comments, it does not work on stackedinline / tabularinline. Is there any other way to achieve this? I have a list of objects attached to the model, and I just want to show them in the representation of the model details without the possibility of changing values.

+10
django django-admin
Jan 29 '10 at 9:09 on
source share
3 answers

If you are using Django 1.3 or later; there is a ModelAdmin.readonly_fields attribute that you could use.

InlineModelAdmin inherited from ModelAdmin , so you can use it from your built-in subclass.

+17
Jan 29 '10 at 12:09 on
source share

Today I faced the same problem. Here is my solution. This is an example read-only field for a ForeignKey value:

 class MySelect(forms.Select): def render(self, name, value, attrs=None, choices=()): s = Site.objects.get(id=value) return s.name class UserProfileInlineForm(forms.ModelForm): site = forms.ModelChoiceField(queryset=Site.objects.all(), widget=MySelect) class UserProfileInline(admin.StackedInline): model = UserProfile form = UserProfileInlineForm 
+2
Feb 16 '10 at 23:45
source share

As with jQuery, it seems you can achieve this by changing the attr called "disabled" (works in my Safari, OK, we are now in 2013 :-)). Example below:

 def get_form(self, request, obj=None, **kwargs): result = super(<your ModelAdmin class here>, self).get_form(request, obj=obj, **kwargs) result.base_fields[<the select field you want to disable>].widget.attrs['disabled'] = 'disabled' return result 
0
Jun 13 '13 at 13:11
source share



All Articles