readonly_fields depending on request

67 views
Skip to first unread message

Roberto López López

unread,
Oct 10, 2013, 8:45:23 AM10/10/13
to django...@googlegroups.com
Hi,

In my project I make use of django-admin. I need to have different
readonly_fields depending on the user is connected, thus administrators
will be able to modify all fields, and other users less fields. Which
method do I have to override? I have already tried
ModelAdmin.get_readonly_fields() and ModelAdmin.get_form(). But no success.

Any other suggestions?

Thanks!

Roberto


Timothy W. Cook

unread,
Oct 10, 2013, 9:00:55 AM10/10/13
to django...@googlegroups.com
Can you post the code you tried with get_form()?  It should be the solution.




    Roberto


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5256A163.3000907%40uni.no.
For more options, visit https://groups.google.com/groups/opt_out.



--
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc           +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

Roberto López López

unread,
Oct 10, 2013, 9:04:34 AM10/10/13
to django...@googlegroups.com
This is, for example, my code overriding get_readonly_fields():

def get_readonly_fields(self, request, obj=None):
readonly = super(NewsAdmin, self).get_readonly_fields(request, obj)
if request.user.groups.filter(name__exact='administration') or
request.user.is_superuser:
return readonly
else:
import itertools
return itertools.chain(readonly, ('is_featured',))
--

Roberto L�pez L�pez
System Developer
Parallab, Uni Computing
+47 55584091

Timothy W. Cook

unread,
Oct 10, 2013, 10:52:09 AM10/10/13
to django...@googlegroups.com
On Thu, Oct 10, 2013 at 10:04 AM, Roberto López López <robert...@uni.no> wrote:
This is, for example, my code overriding get_readonly_fields():

    def get_readonly_fields(self, request, obj=None):
        readonly = super(NewsAdmin, self).get_readonly_fields(request, obj)
        if request.user.groups.filter(name__exact='administration') or
request.user.is_superuser:
            return readonly
        else:
            import itertools
            return itertools.chain(readonly, ('is_featured',))



I do not understand your logic as it compares to your description of the problem. 

For your described problem:
If the user is_superuser then you shouldn't set read_only at all.

If a user is in a specific set of users then set read_only= ['field1', 'field2',] as appropriate. 

It is a pretty simple problem it looks like you are making it far too complex. ????




 



 

On 10/10/2013 02:45 PM, Roberto López López wrote:
> Hi,
>
> In my project I make use of django-admin. I need to have different
> readonly_fields depending on the user is connected, thus administrators
> will be able to modify all fields, and other users less fields. Which
> method do I have to override? I have already tried
> ModelAdmin.get_readonly_fields() and ModelAdmin.get_form(). But no success.
>
> Any other suggestions?
>
> Thanks!
>
>     Roberto
>
>


--

Roberto López López

System Developer
Parallab, Uni Computing
+47 55584091
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Roberto López López

unread,
Oct 10, 2013, 11:00:54 AM10/10/13
to django...@googlegroups.com
Hi,

It was something like that:


    def get_form(self, request, obj=None, **kwargs):
        if not request.user.groups.filter(name__exact='administration') and not request.user.is_superuser:
            import itertools
            self.readonly_fields = itertools.chain(self.readonly_fields, ('is_featured', ))
        return super(NewsAdmin, self).get_form(request, obj, **kwargs)


Now I see that this was slightly different than what I did with get_readonly_fields...

For more options, visit https://groups.google.com/groups/opt_out.


-- 

Roberto López López
System Developer
Parallab, Uni Computing
+47 55584091

Roberto López López

unread,
Oct 15, 2013, 9:49:58 AM10/15/13
to django...@googlegroups.com

Hi Timothy, thanks for your advice.

I'm sorry, I've been quite busy lately on other stuff so I had to leave this apart. You are right, maybe I am overcomplicating things. I have simplified my code to:

    def get_readonly_fields(self, request, obj=None):
        if not request.user.groups.filter(name__exact='administration'):
            return ('is_featured', )
        return ()

That should make the "featured" checkbox read only for those users not in the administration group. But it doesn't, when the flow reaches the line return ('is_featured', ), the user can check/uncheck the box in the admin interface. Do you see any flaw here? Thanks again!

Roberto

For more options, visit https://groups.google.com/groups/opt_out.


-- 

Roberto López López
System Developer
Parallab, Uni Computing
+47 55584091
Reply all
Reply to author
Forward
0 new messages