how do I show a non-editable field in the admin site

14 views
Skip to first unread message

Eernst Bunders

unread,
Jun 18, 2009, 9:47:53 AM6/18/09
to Django users
Hello

I am new to django, and am in the process of discovering what it can
and can not do (out of the box). We are primarily interested in jango
as a content generation tool, so I am focusing on modeling and using
the admin site.
One thing I can't realy figure out is how to show fields in admin
forms without editing them.
One good example for such a feature is the Choice.votes field of poll
tutorial: Poll votes should normally not be editable for site
editors, but you would like to show it.
The same applies to meta information like 'author' or 'creationdate'.

The logical approach would be to set the Field.editable field to
False, and then include the field in your ModelAdmin specialization.
But this yields an error.

I'v been through the sourcecode for a bit to find an answer to this,
but so far no luck, so I thought I try this. It seems such an obvious
feature that i can't believe it is missing.

can anybody help?

regards,

Ernst Bunders

ankit rai

unread,
Jun 19, 2009, 12:56:16 AM6/19/09
to django...@googlegroups.com
use readonlyadmin in your admin.py.U can easily get it on search

Ernst Bunders

unread,
Jun 19, 2009, 5:26:45 AM6/19/09
to django...@googlegroups.com
hello

thanks for your reply, but it is a bit to brief for me. I looked
reedonlyadmin up in google and the only thing i find is
http://code.djangoproject.com/wiki/ReadOnlyAdmin, which is a proposal.
So: i guess this has been implemented, but it is not documented in the
online documentation. So what version of django has this feature, and
where can i see an example.

thanks,

Ernst
--
Ernst bunders
Ontwikkelaar VPRO

ankit rai

unread,
Jun 19, 2009, 7:45:22 AM6/19/09
to django...@googlegroups.com
hello,

save this code in a file (python file) in your project and import this in admin.py were u want the readonly field.modify
class Mymodel(admin.ModelAdmin):   to class Mymodel(ReadOnlyAdminFields,admin.ModelAdmin).

and inside ur admin.py below fields wirte readonly =('field','field1','field2')

from django import forms
from django.utils.safestring import mark_safe
from datetime import datetime

class ReadOnlyWidget(forms.Widget):
    def __init__(self, original_value, display_value):
        self.original_value = original_value
        self.display_value = display_value

        super(ReadOnlyWidget, self).__init__()

    def render(self, name, value, attrs=None):
        if self.display_value is not None:
            return unicode(self.display_value)
        return unicode(self.original_value)

    def value_from_datadict(self, data, files, name):
        return self.original_value


#to make fields  foreignkey readonly
class ReadOnlyAdminFields(object):
    def get_form(self, request, obj=None):
        form = super(ReadOnlyAdminFields, self).get_form(request, obj)

        if hasattr(self, 'readonly') and obj is not None:
            for field_name in self.readonly:
                if field_name in form.base_fields:
                    if hasattr(obj, 'get_%s_display' % field_name):
                        display_value = getattr(obj, 'get_%s_display' % field_name)()
                    else:
                        display_value = None
           
                    if getattr(obj, field_name).__class__ in [unicode , long, int, float, datetime, list]:
           
                        form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name), display_value)
                    else:
           
                        form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name).id, display_value)
            form.base_fields[field_name].required = False
        return form


        return form

Ernst Bunders

unread,
Jun 19, 2009, 8:15:23 AM6/19/09
to django...@googlegroups.com
Thank you, Ankit

This is exactly what i need. I'll try it out.

regards,

Ernst
Reply all
Reply to author
Forward
0 new messages