Readonly on subset of forms in Admin inlines

1,140 views
Skip to first unread message

Peter Phillips

unread,
Jan 19, 2011, 9:27:19 PM1/19/11
to Django users
Hello,

Is there a straightforward way to set fields to read only for a subset
of the forms in an inline formset in the Admin? I'd like to set some
fields to read only on my inline form for rows that were not created
by the user. However, when I set the fields to read only, it of course
affects all rows/forms in the inline, preventing the user from editing
their own rows. Do I need to do a custom formset for
InlineModelAdmin.formset?

Thomas

unread,
Jan 20, 2011, 4:24:38 AM1/20/11
to django...@googlegroups.com


hi,

maybe this provides a proper solution for you:
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields


good luck,
TR

------------------------------------------------

http://thoreg.org


Peter Phillips

unread,
Jan 23, 2011, 6:07:39 AM1/23/11
to Django users
Thanks for the reply!

I probably didn't explain the problem properly. I have been using the
ModelAdmin.readonly_fields option, however it hasn't been doing quite
what I need for inline formsets. In the case of my inline form, when I
set some of the fields to readonly, they end up read only for all
forms in the formset. That is, every new row that's added to the
inline formset and all existing ones end up with read only fields.
What I was hoping to do was override a method to dynamically set some
fields as read only, but only for some of the forms in the inline
formset, not all of them.

The following code means that once a row/form in the inline was not
created by the request.user, all rows/forms become readonly. What I'd
like is to have fields only in the rows they did not create be
readonly, leaving the ones that they did create editable.

I wasn't sure whether there was perhaps an easy way to loop through
the forms in the inline formset and set options in the admin for each
of them separately.

class IssuesInline(admin.TabularInline):
model = Issues
extra = 0
can_delete = False
fields = ('risk', 'issue', 'priority', 'l1', 'c1', 'notes')
exclude = ('added_by', 'modified_by')
readonly_fields = ('lbyc')

def get_readonly_fields(self,request,obj=None):
if request.user.username!=obj.added_by:
return ('risk', 'issue', 'priority', 'l1', 'c1',
'notes',) + self.readonly_fields
return self.readonly_fields

Thanks again!

On Jan 20, 8:24 pm, Thomas <tho...@googlemail.com> wrote:
> Am 20.01.2011 um 03:27 schrieb Peter Phillips:
>
> > Hello,
>
> > Is there a straightforward way to set fields to read only for a subset
> > of the forms in an inline formset in the Admin? I'd like to set some
> > fields to read only on my inline form for rows that were not created
> > by the user. However, when I set the fields to read only, it of course
> > affects all rows/forms in the inline, preventing the user from editing
> > their own rows. Do I need to do a custom formset for
> > InlineModelAdmin.formset?
>
> hi,
>
> maybe this provides a proper solution for you:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contri...

Vinicius Massuchetto

unread,
May 12, 2011, 9:28:18 AM5/12/11
to Peter Phillips, Django Users
Hi! Did you find any solution for this? I'm trying to achieve a
similar behavior for inlines, as posted in another recent thread.

Thanks.
--
Vinicius Massuchetto

On Jan 23, 8:07 am, Peter Phillips <pxph...@gmail.com> wrote:
> Thanks for the reply!
>
> I probably didn't explain the problem properly. I have been using the
> ModelAdmin.readonly_fields option, however it hasn't been doing quite
> what I need for inline formsets. In the case of my inline form, when I

> set some of the fields toreadonly, they end up read only for all


> forms in the formset. That is, every new row that's added to the
> inline formset and all existing ones end up with read only fields.
> What I was hoping to do was override a method to dynamically set some
> fields as read only, but only for some of the forms in the inline
> formset, not all of them.
>
> The following code means that once a row/form in the inline was not

> created by the request.user, all rows/forms becomereadonly. What I'd
> like is to have fields only in the rows they did not create bereadonly, leaving the ones that they did create editable.

Reply all
Reply to author
Forward
0 new messages