admin.py: putting "if condition" in ModelAdmin according to username

250 views
Skip to first unread message

Vittorio

unread,
Oct 9, 2013, 7:32:09 AM10/9/13
to django...@googlegroups.com
For the sake of simplicity let's suppose I have

models.py

class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)

Under admin I would like that some username (say "vic" and "lucky") can edit every field of the change form while other users ("tom","sue") can only visualize the same data without modification. I think to put some "if condition" in admin.py of this kind


class BookOption(admin.ModelAdmin):
if username in ["tom", "sue"]:
readonly_fields = ['title','authors','publisher']
fields=(('title'),('authors', 'publisher'))..
.....
admin.site.register(Book,BookOption)


How can I do it?

Ciao
Vittorio

Rafael E. Ferrero

unread,
Oct 9, 2013, 8:14:35 AM10/9/13
to django...@googlegroups.com
In Admin Site, you can manage users permissions for Add, Delete, Modify of every model on your site.


2013/10/9 Vittorio <ml-...@de-martino.it>
Vittorio

--
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/F0A087D2-8E11-4B0B-8D95-6FC131854994%40de-martino.it.
For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

Victor

unread,
Oct 9, 2013, 10:06:57 AM10/9/13
to django...@googlegroups.com
Yes, I know but unfortunately if in Admin Site you say that a user cannot Add, Delete, and Modify a model if that user is connected he/she doesn't see the model itself in the list but only those models for which it has some kind of permission. I instead would like to make the model visible but not editable for that user.

Ciao
Vittorio
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJJc_8U3EbZXHVSucFndA25V%3DuYyen%3DGgUsyHmzLncD5DuGi2w%40mail.gmail.com.

Bill Freeman

unread,
Oct 9, 2013, 10:42:02 AM10/9/13
to django-users
It's easy, if you don't insist on making it part of the admin, to do this with a custom view.  It can pay attention to whatever permissions you like.


Timothy W. Cook

unread,
Oct 9, 2013, 1:18:34 PM10/9/13
to django...@googlegroups.com
In the ModelAdmin I use a get_form() to test for conditions on the object. Specifically, if this item has been published then make it read_only.  You should be able to test for users as well.

I had to add the try/except in order to add new objects because obj.published doesn't exist on a new object. 

See:
 

Here is a snippet: 
    def get_form(self, request, obj=None, **kwargs):
        try:
            if obj.published:
                self.readonly_fields = ['prj_name','published','schema_code','data_name','description','sem_attr','resource_uri','asserts',
                                        'simple_units','coded_units','normal_status','reference_ranges','min_inclusive','max_inclusive',
                                        'min_exclusive','max_exclusive','total_digits','fraction_digits','magnitude','error','accuracy','magnitude_status',]
        except (AttributeError, TypeError) as e:
            self.readonly_fields = ['published','schema_code']
        return super(DvQuantityAdmin, self).get_form(request, obj, **kwargs)




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
Reply all
Reply to author
Forward
0 new messages