views:

751

answers:

2

I use this snippet to show several fields in my admin backend as readonly, but as noticed 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 a model and just want to show it in the model's details view without the possibility to change values.

+2  A: 

If you have the possibility(and courage) to run your code against Django trunk; theres an attribute named ModelAdmin.readonly_fields which you could use.

InlineModelAdmin inherits from ModelAdmin, so you should be able to use it from your inline subclass.

drmegahertz
thanks for your answer, unfortunately, it's a productive system where i can't use a dev-version.
schneck
A: 

I've encountered the same problem today. Here is my solution. This is example of read-only field for the 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
Ivan Virabyan