Tuesday, October 15, 2013

Re: readonly_fields depending on request


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






On 10/10/2013 04:52 PM, Timothy W. Cook wrote:
On Thu, Oct 10, 2013 at 10:04 AM, Roberto López López <roberto.lopez@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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/5256A5E2.9020505%40uni.no.



--
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
--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CA%2B%3DOU3U69QzDSbc0qyJ_zbgA3BYHCi5zK3%3DaAra-saAq%2Bvj3EA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.


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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate