Wednesday, November 18, 2015

Re: Making a field readonly according to the user in admin.py

These three references should get you started.

get_queryset[1] is useful for filtering the entire display based on the
request user.

readonly_fields[2] is a simple list of fields which are readonly for
everyone.

get_readonly_fields[3] is an Admin callable which can be overridden by
your own method provided your method has the same (request, obj)
signature. In admin.py you would say get get_readonly_fields =
my_get_readonly_fields

I agree with Jani that you will be better off in the long run building
your own interface ... but ymmv

[1]
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_queryset

[2]
https://docs.djangoproject.com/en/1.8/ref/contrib/admin/#django.contrib.admin.ModelAdmin.readonly_fields

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

Mike

On 18/11/2015 9:15 PM, Victor wrote:
> Dear experts,
>
> models.py
>
> class Book(models.Model):
> title = models.CharField(max_length=100)
> author = models.CharField(max_length=100)
> quantity = models.IntegerField(db_column='quantitity',default=0)
> class Meta:
> db_table="book"
>
> admin.py
>
> class BookOption(admin.ModelAdmin):
> list_display = ('title', 'author', 'quantity')
> fields=(('title', 'author'), ('quantity'))
> order_by= ['title', ]
>
> admin.site.register(Book,BookOption)
>
>
> I'm trying to explain my problem with the simple example above.
> Let's suppose that I have two staff users 'victor' and 'roby' and I want that the fields 'author and 'quantity' be readonly when user 'roby' is logged in and readwrite for user 'victor'
> How can I achieve this result if possible with something of the following simple kind
>
>
> class BookOption(admin.ModelAdmin):
> if (user is 'roby'):
> readonly_fields=['quantity',]
> list_display = ('title', 'author', 'quantity')
> fields=(('title', 'author'), ('quantity'))
> order_by= ['title', ]
>
> Thanks
> 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/564CD7CD.1040707%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment