Thank you, Ankit

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

regards,

Ernst

On Fri, Jun 19, 2009 at 1:45 PM, ankit rai<ankit9...@gmail.com> wrote:
> 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
>
> On Fri, Jun 19, 2009 at 2:56 PM, Ernst Bunders <ebund...@gmail.com> wrote:
>>
>> 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
>>
>> On Fri, Jun 19, 2009 at 6:56 AM, ankit rai<ankit9...@gmail.com> wrote:
>> > use readonlyadmin in your admin.py.U can easily get it on search
>> >
>> > On Thu, Jun 18, 2009 at 7:17 PM, Eernst Bunders <ebund...@gmail.com>
>> > wrote:
>> >>
>> >> 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
>> >>
>> >>
>> >
>> >
>> > >
>> >
>>
>>
>>
>> --
>> Ernst bunders
>> Ontwikkelaar VPRO
>>
>>
>
>
> >
>



-- 
Ernst bunders
Ontwikkelaar VPRO

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to