Wednesday, October 9, 2013

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

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)



On Wed, Oct 9, 2013 at 11:06 AM, Victor <vdemart@gmail.com> wrote:
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

Il giorno 09/ott/2013, alle ore 14:14, Rafael E. Ferrero ha scritto:

> In Admin Site, you can manage users permissions for Add, Delete, Modify of every model on your site.
>
>
> 2013/10/9 Vittorio <ml-vic@de-martino.it>
> 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
>
> --
> 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/F0A087D2-8E11-4B0B-8D95-6FC131854994%40de-martino.it.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
> --
> Rafael E. Ferrero
>
> --
> 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/CAJJc_8U3EbZXHVSucFndA25V%3DuYyen%3DGgUsyHmzLncD5DuGi2w%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
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/B4D514E2-7FA0-4CB3-A83E-82442A8DAFC6%40gmail.com.
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

--
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%3DOU3WnGZrh3xH1qHHtExAeAZTFNBOzvSN3eF67KVBsQNKvWg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate