display readonly or not

34 views
Skip to first unread message

Tony Peña

unread,
Aug 10, 2015, 10:49:03 PM8/10/15
to django...@googlegroups.com
hi

i have this on my model.admin

class MyDomainAdmin(admin.ModelAdmin):
    list_display = ('domain', 'enabled', 'avscan', 'spamassassin', 'max_accounts')
    list_filter = ('enabled',)
    exclude = ('uid', 'gid', 'maildir')

# this part is my pseudo-code logic I want but i can't solved.
    if not user.is_superuser
        readonly_fields = ('max_accounts',)

how can i set readonly for users staff? but normally modify for only superusers?
--
Antonio Peña
Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
Fingerprint: 74E6 2974 B090 366D CE71  7BB2 6476 FA09 8B02 1001

Mike Dewhirst

unread,
Aug 11, 2015, 1:24:32 AM8/11/15
to django...@googlegroups.com
On 11/08/2015 7:25 AM, Tony Peña wrote:
> hi
>
> i have this on my model.admin
>
> class MyDomainAdmin(admin.ModelAdmin):
> Â Â list_display = ('domain', 'enabled', 'avscan', 'spamassassin',
> 'max_accounts')
> Â Â list_filter = ('enabled',)
> Â Â exclude = ('uid', 'gid', 'maildir')
>
> # this part is my pseudo-code logic I want but i can't solved.
> Â Â if not user.is_superuser
> Â Â Â Â readonly_fields = ('max_accounts',)
>

There is a get_read_only() method in the admin.ModelAdmin class. It
normally returns self.readonly_fields but you can give it a callable.

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields

As well as defining any readonly fields (for everyone) you want
something like ...

def ro_fields(self, request, obj=None):
if obj is None or request.user.is_superuser:
return self.readonly_fields
else:
return self.model._meta.get_all_field_names()

... then get_readonly_fields = ro_fields should make it happen like your
pseudocode.

Mike



> how can i set readonly for users staff? but normally modify for only
> superusers?
> --
> Antonio Peña
> Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
> <https://pgp.mit.edu/pks/lookup?search=0x8B021001&op=index&fingerprint=on&exact=on>
> Fingerprint: 74E6 2974 B090 366D CE71Â 7BB2 6476 FA09 8B02 1001
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/CALBaCdsUys33wv1n6tMaiKvEj%2BXcZRPytO_os9xNFW4ij0Vu0w%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CALBaCdsUys33wv1n6tMaiKvEj%2BXcZRPytO_os9xNFW4ij0Vu0w%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Tony Peña

unread,
Aug 11, 2015, 3:07:35 AM8/11/15
to django...@googlegroups.com
hmmm.. not work

the field max_account must be modify for superuser not for the staff.

class VeximDomainAdmin(admin.ModelAdmin):
    list_display = ('domain', 'enabled', 'avscan', 'spamassassin', 'max_accounts')
    list_filter = ('enabled',)
    exclude = ('uid', 'gid', 'pipe', 'maildir', 'blocklists', 'complexpass')
    readonly_fields = ('max_accounts',)

    def ro_fields(self, request, obj=None):
        if obj is None or request.user.is_superuser:
            return self.readonly_fields
        else:
            return self.model._meta.get_all_field_names()

    get_readonly_fields = ro_fields


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.

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



--
Antonio Peña

Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
Fingerprint: 74E6 2974 B090 366D CE71  7BB2 6476 FA09 8B02 1001

Daniel Roseman

unread,
Aug 11, 2015, 5:17:20 AM8/11/15
to Django users
On Tuesday, 11 August 2015 06:24:32 UTC+1, Mike Dewhirst wrote:
 
There is a get_read_only() method in the admin.ModelAdmin class. It
normally returns self.readonly_fields but you can give it a callable.

https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields

As well as defining any readonly fields (for everyone) you want
something like ...

def ro_fields(self, request, obj=None):
     if obj is None or request.user.is_superuser:
         return self.readonly_fields
     else:
         return self.model._meta.get_all_field_names()

... then get_readonly_fields = ro_fields should make it happen like your
pseudocode.

Mike

Why do you define a separate method, then alias to get_readonly_fields? Why not override the method directly?
--
Daniel. 

Tony Peña

unread,
Aug 11, 2015, 6:37:04 AM8/11/15
to django...@googlegroups.com
because i don't know how... that's why i ask :)

--
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.

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



--
Antonio Peña

Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
Fingerprint: 74E6 2974 B090 366D CE71  7BB2 6476 FA09 8B02 1001

Mike Dewhirst

unread,
Aug 11, 2015, 7:28:34 AM8/11/15
to django...@googlegroups.com
On 11/08/2015 7:17 PM, Daniel Roseman wrote:
> On Tuesday, 11 August 2015 06:24:32 UTC+1, Mike Dewhirst wrote:
> Â
>
> There is a get_read_only() method in the admin.ModelAdmin class. It
> normally returns self.readonly_fields but you can give it a callable.
>
> https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields
> <https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields>
>
>
> As well as defining any readonly fields (for everyone) you want
> something like ...
>
> def ro_fields(self, request, obj=None):
> Â Â Â if obj is None or request.user.is_superuser:
> Â Â Â Â Â return self.readonly_fields
> Â Â Â else:
> Â Â Â Â Â return self.model._meta.get_all_field_names()
>
> ... then get_readonly_fields = ro_fields should make it happen like
> your
> pseudocode.
>
> Mike
>
>
> Why do you define a separate method, then alias to get_readonly_fields?
> Why not override the method directly?

Because in some places/models in the admin I don't want to override the
method; and in all (my) cases there needs to be readonly "modified" and
"modified_by" fields for superusers as well.

Further, I put my ro_fields method in a common module and import it
where required in a number of apps in that project.

Cheers

Mike

> --
> Daniel.Â
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/b43856c6-4e6f-435a-bee2-886110d2aa1a%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/b43856c6-4e6f-435a-bee2-886110d2aa1a%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages